将字符串中的每个单词输出到新行
Output each word in string to a new line
我正在设计一个函数,它有一个输入参数:字符串。该函数获取该字符串的内容,并将每个单词输出到新行。目前,这个函数除了输出字符串中的最后一个单词外,什么都做。这是功能代码:
void outputPhrase(string newPhrase)
{
string ok;
for (int i = 0; i < newPhrase.length(); i++)
{
ok += newPhrase[i];
if (isspace(newPhrase.at(i)))
{
cout << ok << endl;
ok.clear();
}
}
}
试试这个:
for (int i = 0; i < newPhrase.length(); i++)
{
ok += newPhrase[i];
if (isspace(newPhrase.at(i)) || i==newPhrase.length()-1)
{
cout << ok << endl;
ok.clear();
}
}
你可以使用这个函数来完成你的任务,
void split(string newPhrase)
{
istringstream iss(newPhrase);
do
{
string sub;
iss >> sub;
cout << sub << endl;
} while (iss);
}
请记住包括<string>和<代码中的sstream>。
相关文章:
- 如何根据单词在文本中出现的概率输出单词
- 当字符串是某个单词时给出输出?
- 输出返回编号。等于输入字符串的单词的字符串数
- 如何让变量随机输出四个单词之一
- 如何在函数中输入数字以输出单词?
- CPP - strlen 不会输出多个单词的整个长度 [仅限 C 字符串]
- 如何输出以姓氏开头的全名?如果名字或姓氏由单词组成,该怎么办?
- 如何输出字符串第二个单词的第一个字母?
- C++ 输出在读取文本文件时跳过最后一个单词
- C++输出文件中缺少单词
- 在输出时,我对如何将数量和单词组合在一行感到非常困惑
- 如何计算出现在字符串和输出行中的唯一单词?C
- C++,我想在字符串中搜索一个字符,并输出包含该字符的所有单词
- 给出错误的输出:如何将字符串分解为字典单词
- 如何在先前读取行中找到一些单词并在C 中的输出读/写字符串中删除它
- 对于每个单词输出一个对列表,行号和该行的字数
- 如何从字符串中每行输出一个单词
- 编写代码将给定的数字转换为单词(例如1234作为输入应输出一千二百三十四)
- C++文件输出只接受第一个单词
- 将字符串中的每个单词输出到新行