用c加加写一个代码,读入一个字符串的,反向输出每一个字符
时间: 2024-05-11 20:18:57 浏览: 8
下面是用C++实现的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "请输入一个字符串:";
getline(cin, str); // 可以读入带空格的字符串
for(int i = str.length() - 1; i >= 0; i--) {
cout << str[i];
}
cout << endl;
return 0;
}
```
运行结果:
```
请输入一个字符串:Hello, world!
!dlrow ,olleH
```
相关问题
运用C加加编写一个程序输入一个字符串求字符串中字符的个数并输出
以下是一个简单的 C++ 程序,可以实现输入一个字符串,统计其中的字符个数并输出:
```c++
#include <iostream>
#include <string>
int main() {
std::string str;
std::cout << "请输入一个字符串:" << std::endl;
std::getline(std::cin, str);
int count = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] != ' ') { // 排除空格
count++;
}
}
std::cout << "字符个数为:" << count << std::endl;
return 0;
}
```
这个程序中,首先使用 `std::getline()` 函数读入一行字符串,然后遍历字符串中的每个字符,如果不是空格,则计数器加 1。最后输出字符个数即可。
编写一个c加加程序 输入3个字符串按由小到大的顺序输出
好的,这个问题属于程序设计领域。以下是一个示例程序:
```
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str1, str2, str3;
cout << "请输入3个字符串:" << endl;
cin >> str1 >> str2 >> str3;
string arr[] = { str1, str2, str3 };
sort(arr, arr + 3);
cout << "按顺序输出结果为:" << endl;
for (int i = 0; i < 3; i++) {
cout << arr[i] << endl;
}
return 0;
}
```
请注意,这只是一个简单的示例程序,您可以根据自己的需求进行修改和完善。