统计英文句子中有多少个英文单词 单词之间用空格分开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include<iostream> #include<string.h> using namespace std; void main() { int i,j=0;
char a[100]; cout<<"请输入一个英文句子"<<endl; gets(a); puts(a); for(i=0;i<strlen(a);i++) { if(a[i]==32) {j++;} } cout<<"该句子中有"<<j+1<<"个单词"<<endl;
}
#include<iostream> #include<string.h> using namespace std; void main() { void sum(char *words); char words[50]; cout<<"请给出一行字符"<<endl; gets(words); sum(words);
} void sum(char *words) { int _sum=0; for(int i=0;i<strlen(words);i++) { if(words[i]==32) _sum++;
} cout<<"这行字符中有"<<_sum+1<<"个单词"<<endl;
}
给出一行字符,统计其中单词的个数,单词之间用空格隔开 #include<iostream>
usingnamespace std;
void main()
{
char s[100];
int sum=0;
cout<<"please inputs:";
gets(s);
for(int i=0;s[i]!='\0';i++)
if(s[i]!=' '&&s[i+1]==''||s[i+1]=='\0')
sum++;
cout<<"sum中有单词sum="<<sum<<"个"<<endl;
}
|