如何统计字符串中不同单词的个数,C++语言进行编辑

2025-05-11 17:26:15
推荐回答(2个)
回答(1):

#include "stdafx.h"
#include "iomanip.h"//
#include //
struct word{
char c[30];
int n;
}w[1000];
void main(void){/*主程序*/
char b[30],Str[30000];
int i=0,m=1,k=0,flag=0,x=0;
cout << "Type a short text...\nStr=";
while(cin.get(Str[x]),Str[x++]!='\n');
Str[x-1]='\0';
x=0;
while(Str[x]){
if('A'<=Str[x] && Str[x]<='Z' || 'a'<=Str[x] && Str[x]<='z' || Str[x]=='-'){
b[i++]=Str[x];
flag=1;
}
else if(flag){
b[i]='\0';
flag=m=0;
for(i=0;i if(strcmp(b,w[i].c)==0){
m=1;
break;
}
}
if(!m){
w[k].n=1;
strcpy(w[k++].c,b);
}
else w[i].n++;
i=0;
}
x++;
}

for(i=0;i cout << w[i].c << " " << w[i].n << endl;
cout << endl;
}

回答(2):

是指单词?有空格或者其他符号分割?例如 "Love is zero"这种?