我如何将句子分成一个单词
how do i dividing the sentence into a word
如何在 C++ 中划分句子,例如:
来自cin的输入(他说,"这不是一个好主意"。
到
他
说
那
s
不
一个
好
想法
要测试字符是否为字母,请使用语句 (ch>='a' && ch <='z') ||(ch>='A' && ch <='Z')。
您可以按空格拆分字符串,然后检查每个单词是否包含 A-z 以外的任何字符。 如果有,请擦除它。这里有一个提示:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
std::vector<std::string> splitBySpace(std::string input);
std::vector<std::string> checker(std::vector<std::string> rawVector);
int main() {
//input
std::string input ("Hi, My nam'e is (something)");
std::vector<std::string> result = checker(splitBySpace(input));
return 0;
}
//functin to split string by space (the words)
std::vector<std::string> splitBySpace(std::string input) {
std::stringstream ss(input);
std::vector<std::string> elems;
while (ss >> input) {
elems.push_back(input);
}
return elems;
}
//function to check each word if it has any char other than A-z characters
std::vector<std::string> checker(std::vector<std::string> rawVector) {
std::vector<std::string> outputVector;
for (auto iter = rawVector.begin(); iter != rawVector.end(); ++iter) {
std::string temp = *iter;
int index = 0;
while (index < temp.size()) {
if ((temp[index] < 'A' || temp[index] > 'Z') && (temp[index] < 'a' || temp[index] > 'z')) {
temp.erase(index, 1);
}
++index;
}
outputVector.push_back(temp);
}
return outputVector;
}
在此示例中,result
是一个包含此句子单词的向量。
注意:如果您不使用 c++1z,请使用 std::vector<std::string>::iterator iter
而不是 auto iter
相关文章:
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 如何检查流中文件的下一个单词是否为 alpha?
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 计算一个单词在映射中出现的次数 - 使用 transform() - 调用不匹配
- 如何将一个单词拆分成字母,并将它们放入一个无序的列表/集合中
- 将字符串中的单个单词替换为另一个单词
- 如何插入一个单词并将字母放在2D数组中的不同索引中
- C++ ifstream 只读一个单词的数量
- 我如何计算一个单词在数组中重复的次数
- 如何将 c 字符串转换为指针数组,其中每个指针都是 char 数组中的一个单词
- 将 C++ 类属性与分号前面的最后一个单词对齐;在 emacs 中
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我如何在一串随机生成的字母I C 中查找一个单词
- 在字符串中找到最后一个单词
- C++ 输出在读取文本文件时跳过最后一个单词
- 我如何将句子分成一个单词
- 在向量中通过一个单词查找行
- 'Try This'矢量在示例词典中哔哔一个单词。编程原理与实践
- 我怎样才能从字符串中得到一个单词
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词