题目描述 输入一串字符,字符个数不超过100。 判断它们是否构成回文(正着读和反着读一样)。 输入 输入只有一行,包括一串字符.。 输出 输出只有一行,"TRUE" 或者 "FALSE"。 C++
时间: 2024-03-24 13:36:02 浏览: 25
以下是C++实现代码:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
int len = s.size();
bool is_palindrome = true;
for (int i = 0; i < len / 2; i++) {
if (s[i] != s[len - i - 1]) {
is_palindrome = false;
break;
}
}
if (is_palindrome) {
cout << "TRUE" << endl;
} else {
cout << "FALSE" << endl;
}
return 0;
}
```
首先使用`getline()`函数读入一行字符串,然后遍历字符串的前一半字符。将第`i`个字符和第`len-i-1`个字符进行比较,如果不相等,则说明不是回文串。最后根据判断结果输出`TRUE`或`FALSE`即可。
相关问题
检查一个字符串是否是回文(正着读和反着读都一样的字符串),若是就输出yes,否则输出no
A:可以使用双指针法来检查一个字符串是否是回文。具体实现如下:
```
def is_palindrome(s):
i, j = 0, len(s) - 1 # 定义两个指针,i指向字符串开头,j指向字符串结尾
while i < j:
if s[i] != s[j]: # 如果当前指针所指的字符不相等,说明不是回文,返回no
return 'no'
i += 1 # 移动左边的指针
j -= 1 # 移动右边的指针
return 'yes' # 如果执行到这里,说明是回文,返回yes
# 测试
print(is_palindrome('level')) # 输出 yes
print(is_palindrome('hello')) # 输出 no
```
另外,Python也可以通过字符串切片[::-1]来实现字符串反转,从而进行字符串回文的检查:
```
def is_palindrome(s):
return 'yes' if s == s[::-1] else 'no' # 判断原字符串是否等于反转后的字符串
# 测试
print(is_palindrome('level')) # 输出 yes
print(is_palindrome('hello')) # 输出 no
```
用C++编写程序 输入一行字符串,统计其中大写字母,小写字母,空格,数字及其他字符的个数。 判断其是否回文。回文串即正着读和反着读都相同的字符串
以下是用C编写的程序,可以输入一行字符串,统计其中大写字母,小写字母,空格,数字及其他字符的个数,并判断其是否回文。
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[100];
int i, len, upper = 0, lower = 0, space = 0, digit = 0, other = 0;
printf("请输入一行字符串:\n");
fgets(str, 100, stdin);
len = strlen(str) - 1; // 去掉换行符
for (i = 0; i < len; i++) {
if (isupper(str[i])) {
upper++;
} else if (islower(str[i])) {
lower++;
} else if (isspace(str[i])) {
space++;
} else if (isdigit(str[i])) {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("空格:%d\n", space);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
// 判断是否回文
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
printf("不是回文串\n");
return 0;
}
}
printf("是回文串\n");
return 0;
}
```
如果输入的字符串是 "A man a plan a canal Panama",则输出结果为:
```
大写字母:7
小写字母:14
空格:6
数字:0
其他字符:0
是回文串
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)