#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
string str = "Talk is cheap show me the code";
//将字符串分成单个单词字符串
vector<string> res;
string word_str = "";
int i = 0;
char ch = str[i];
while (ch != '\0') //'\0'是字符串的结束标志
{
if (str[i] == ' ') //遇到(' ')空格字符时,就将之前的字符串加入到结果集中
{
res.push_back(word_str);
word_str = "";
++i;
}
word_str += str[i];
++i;
ch = str[i];
if (ch == '\0') //将最后一个单词加入到“单词”数组中
{
res.push_back(word_str);
}
}
for (auto& strr : res)
{
std::cout << strr << ":" << strr.length() << std::endl << std::endl;
}
}
将句子分割成单词 并 输出
最新推荐文章于 2022-08-17 20:01:03 发布