向后移动单词奇词



我写了代码,但没有完全完成。所以我需要反转奇数单词(它们的长度),只传递偶数单词(我的意思是只打印单词并继续),像这样:omar smu sim load输出:omar ums mis dol .这是我的代码:


my output [1]: https://i.stack.imgur.com/Tcirb.png

这是你想要的吗?

#include <iostream>
#include <string>
using namespace std;
void reverse(string& str)//reversing word
{
for (int i = 0; i < str.length() / 2; i++) 
swap(str[i], str[str.length() - i - 1]);
}
int main()
{
string str;
for (; getline(cin, str), !str.empty();)// read all the lines until it is blank
{
if (str.length() % 2)// reverse odd ones
reverse(str);
cout << str << endl;
}
return 0;
}

我在代码末尾删除了不必要的cout,它只是打印一个空字符串。在for循环中,我首先询问输入字符串的长度是否为奇数,然后将其反转,然后打印它。

最新更新