计数句子中单词的字符数
Counting number of character of words in a sentence
我尝试了以下程序。我想要这个
string input = "hi everyone, what's up."
输出:
hi = 2
everyone = 8
whats= 5
up = 2
我确实在句子中计数单词数,但我想计数句子中单词的字符数。
参考stackoverflow中的旧查询...希望这会有所帮助!
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str("Split me by whitespaces");
string buf; // Have a buffer string
stringstream ss(str); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
cout<< buf<<"="<<buf.length() <<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string s="hello there anupam";
int cnt,i,j;
for(i=0;s[i]!=' ';i++) /*Iterate from first character till last you get null character*/
{
cnt=0; /*make the counter zero everytime */
for(j=i;s[j]!=' '&&s[j]!=' ';j++) /*Iterate from ith character to next space character and print the character and keep a count of number of characters iterated */
{
cout<<s[j];
cnt++;
}
cout<<" = "<<cnt<<"n"; /*print the counter */
if(s[j]==' ') /*if reached the end of string break out */
break;
else
i=j; /*jump i to the next space character */
}
return 0;
}
这是您想要的工作演示。我已经在评论中解释了代码。
相关文章:
- 当您在此单词中搜索单词时调整字符数组的大小?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- C ++程序在字符数组中插入单词
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 延迟逐个字符打印单词
- 创建单词对齐的字符向量
- 在字符数组中存储和恢复一组单词
- 如何用单词排列向量中的每个字符升序?
- 如何在字符指针数组中保留指向每个单词的指针?
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
- 需要将单词的文本文件存储到C 中的字符阵列中
- 从字符数组中删除单词
- 忽略给定单词之前的所有字符
- 如何计算C++字符数组中的单词
- 如何用新单词替换未知数量的相同字符
- (C++) 打印字符数组字符串的单词时出现问题
- 计数句子中单词的字符数
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 使用getline,c 中的数组以相反的方式将所有字符单词转动