如何在C++中迭代一个句子的单词
How to iterate over the words of a sentence in C++?
我的输入是"Hello World",目标输出是"olleH dlroW"。
所以我的想法是把句子变成一个变量,然后在句子中的单词上循环,反转每个单词,最后把它们连接成一个新的变量。
我的问题是:如何迭代句子中的单词?
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
string reverseword(string word)
{
string rword;
int size = word.length();
while (size >= 0)
{
rword+= word[size];
size = size -1;
}
return rword;
}
int main()
{
string sentence;
cout<<"Enter the word/sentence to be reversed: ";
cin >> sentence;
string rsentence;
// for every word in the sentence do
{
rword = reverseword(word);
rsentence = rsentence + " " + rword;
}
cout<<rword;
return 0;
}
在迭代一个句子中的单词之前,您需要从输入中读取一个句子。这条线路
cin >> sentence;
阅读一个句子的第一个单词,而不是整个句子。改为使用getline
:
std::getline(std::cin, sentence);
在内存中有了sentence
,您可以使用istream_iterator
逐字迭代,如下所示:
stringstream ss(sentence);
for (auto w = istream_iterator<string>(ss) ; w != istream_iterator<string>() ; w++) {
string &word = *w;
...
}
演示。
这里有一个使用find
和reverse
来实现输出的解决方案:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string sentence;
std::getline(std::cin, sentence);
std::cout << sentence << std::endl;
size_t cpos = 0;
size_t npos = 0;
while((npos = sentence.find(' ', cpos)) != std::string::npos)
{
std::reverse(sentence.begin() + cpos, sentence.begin() + npos);
cpos = npos + 1;
}
std::reverse(sentence.begin() + cpos, sentence.end());
std::cout << sentence << std::endl;
return 0;
}
输入:
this is a nice day
输出:
this is a nice day
siht si a ecin yad
for(short i=0;i<sentence.length();i++){
if(sentence[i] == ' '){
counter++;
i++;
}
words[counter] += sentence[i];
}
注意上面的循环,用空格分隔句子并将其存储到字符串数组words[]
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
string reverseword(string word) // function to reverse a word
{
string rword;
int size = word.length();
while (size >= 0)
{
rword+= word[size];
size = size -1;
}
return rword;
}
int main()
{
string sentence;
cout << "Enter the word/sentence to be reversed: ";
std::getline(std::cin, sentence);
string rsentence;
string words[100];
string rword;
short counter = 0;
for(short i=0; i<sentence.length(); i++){ // looping till ' ' and adding each word to string array words
if(sentence[i] == ' '){
counter++;
i++;
}
words[counter] += sentence[i];
}
for(int i = 0; i <= counter; i++) // calling reverse function for each words
{
rword = reverseword(words[i]);
rsentence = rsentence + " " + rword; // concatenating reversed words
}
cout << rsentence; // show reversed word
return 0;
}
我已经更正了代码。希望这能帮助。。。!!
注意:您正在使用cin读取空格分隔的字符串,这是不可能的。必须使用std::getline(std::cin, sentence)
读取以空格分隔的字符串
您也可以使用std::reverse()
来反转字符串
请参阅拆分字符串的最优雅方法?把你的句子分成记号(单词)然后,迭代新的单词列表以执行任何操作
上面的答案提供了一种将输入转换为单词的方法,即cin >> sentence
返回一个"单词"(所以,只需重复调用它)。
然而,这就提出了什么是"词"的问题。你想把一个计算机结构——字符串——翻译成一种更复杂的形式——单词。所以,当你想要单词时,你必须定义你的意思。它可以像"空格"分隔的子字符串或字符串一样简单,然后使用split
函数,或者一次读取字符串一个单词(cin >> word
)
或者你可能有更严格的要求,比如不能包括标点符号(比如句子末尾的句号)或数字。然后考虑使用Regex和单词模式(如"\w+")。
或者你可能想要字典里的"真实"单词。然后,您需要考虑您的语言环境,将输入解析为块(使用split、Regex或其他方法),并在人类语言词典中查找每个块。
换句话说,"单词"解析只是和您的需求一样简单或复杂。
使用Boost可以使用boost::split
函数:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <boost/algorithm/string.hpp>
int main()
{
std::string sentence = "Hello world";
std::vector<std::string> words;
boost::split(words, sentence, boost::is_any_of(" "));
std::string rsentence;
for (std::string word : words) // Iterate by value to keep the original data.
{
std::reverse(word.begin(), word.end());
rsentence += word + " "; // Add the separator again.
}
boost::trim(rsentence); // Remove the last space.
std::cout << rsentence << std::endl;
return 0;
}
这个答案是我对对抗全球变暖的微薄贡献。
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
int main()
{
std::string sentence;
while (std::getline(std::cin, sentence))
{
auto ws = sentence.begin();
while (ws != sentence.end())
{
while (std::isspace(*ws)) ++ws;
auto we = ws;
while (we != sentence.end() && !std::isspace(*we)) ++we;
std::reverse(ws, we);
ws = we;
}
std::cout << sentence << "n";
}
}
这假设"单词"被定义为"非空白字符的序列"。很容易用不同的字符类代替"非空白",例如,对于字母数字字符,使用std::isalnum
。一个反映真实世界中单词概念的定义,例如在自然语言科学中使用的定义,远远超出了这个答案的范围。
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 创建一个程序,其中句子在C++中全部大写,全部小写
- 我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
- 我怎样才能找到一个句子的n元语法
- 在C++中输入一个句子
- 我如何将句子分成一个单词
- 如何在C++中迭代一个句子的单词
- 将三个文本文件组合成一个文本文件,形成一个句子C++
- 句子中的最后一个单词在句子颠倒后不打印
- 使用QRegularExpression从句子中提取一个十进制数
- 使用QRegularExpression提取一个完整的句子
- 把一个句子分开,然后找回一个特定的单词
- 反转每个单词在一个句子中使用c++需要代码优化我的代码片段
- 在Google Code jam上问的一个c++句子的倒序
- 要求在循环中写一个句子来计算元音,即使循环进行也不会再问
- 如何存储一个完整的句子与空白的字符串在cpp