如何使用 c++ 反转句子字符串中的每个单词,例如"I love computer science"
How do I reverse each word in a sentence string like "I love computer science" using c++
我正在尝试反转我输入的句子中的每个单词。
例如:输入= 我喜欢计算机科学
输出 = I evol retupmoc ecneicS
但这是上面输入的实际输出:
输出 = ecneicS reupmoc evol I
我不知道如何解决它,请帮忙。非常感谢你。
#include <iostream>
#include <string>
using namespace std;
// Prototype
void swap (char&, char&);
int main()
{
// Vars
string sentence;
// Input
cout << "Welcome to Reverse-the-words" << endl;
cout << "Enter your sentence, word, or phrase" << endl;
getline(cin,sentence);
for (int i=0; i < sentence.length()/2; i++)
{
swap (sentence [i], sentence [sentence.length()-i-1]);
}
// Output
cout << "Your phrase with all of the words reversed is " << endl;
cout << sentence << endl ;
return 0;
}
void swap(char &v1, char &v2)
{
char temp;
temp = v1;
v1= v2;
v2=temp
}
更新代码以反转句子中的每个单词:
#include <iostream>
#include <string>
using namespace std;
// Prototype
void swap (char&, char&);
int main()
{
// Vars
string sentence;
// Input
cout << "Welcome to Reverse-the-words" << endl;
cout << "Enter your sentence, word, or phrase" << endl;
getline(cin,sentence);
int word_begin = 0;
for (int i=0; i <=sentence.length(); i++)
{
if (!isalpha(sentence[i]))
{
for (int j = word_begin; j < (i - (i - word_begin)/2); j++)
{
swap(sentence[j], sentence[i - j + word_begin - 1]);
}
word_begin = i + 1;
}
}
// Output
cout << "Your phrase with all of the words reversed is " << endl;
cout << sentence << endl ;
return 0;
}
void swap(char &v1, char &v2)
{
char temp;
temp = v1;
v1= v2;
v2 = temp;
}
可以直接使用 std::reverse
#include <iostream>
#include <string>
#include <sstream>
#include<algorithm>
using namespace std;
int main()
{
// Vars
string sentence;
// Input
cout << "Welcome to Reverse-the-words" << endl;
cout << "Enter your sentence, word, or phrase" << endl;
getline(cin, sentence);
istringstream iss(sentence);
string word;
cout << "Your phrase with all of the words reversed is " << endl;
while (iss >> word) {
std::reverse(word.begin(), word.end());
cout<<word.c_str()<<" ";
}
return 0;
}
输出
Welcome to Reverse-the-words
Enter your sentence, word, or phrase
I love science
Your phrase with all of the words reversed is
I evol ecneics
获取句子后,将句子中的每个单词放入新变量中,然后反转并打印出来。
提示:您需要在 for 循环中有一个 for 循环。
for word in sentence:
for character in word:
/* your code to reverse the word */
/* print the word */
使用标准库始终是一种很好的做法。以下是我可以想出的在保持连续空格的同时反转单词的方法:
#include <string>
#include <algorithm>
std::string& reverse_words(std::string& sentence) {
auto first = sentence.find_first_not_of(' ');
auto last = sentence.find_first_of(' ', first);
while (last < std::string::npos) {
std::reverse(&sentence[first], &sentence[last]);
first = sentence.find_first_not_of(' ', last);
last = sentence.find_first_of(' ', first);
}
std::reverse(&sentence[first], &sentence[sentence.size()]);
return sentence;
}
也许有一种方法可以写得更优雅,但我想你会明白的。
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 将数字转换为字母(例如:123 转换为一二三)
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 如何计算数据类型的范围,例如int
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 如何使用 c++ 反转句子字符串中的每个单词,例如"I love computer science"
- 编写代码将给定的数字转换为单词(例如1234作为输入应输出一千二百三十四)