#include<iostream>
using namespace std;
struct st
{
char str[100];
int n; //存个数
}b[100]; //all words in this centence should be store in every characters, and the longest word should be under 100, and most can store 100 words
int check(char *a, char *b)
{
int i,j;
for (i=0,j=0; a[i]&&b[j]; i++,j++)
{
if (a[i]==b[j] || a[i]+32==b[j] || a[i]-32==b[j])
continue;
else
break;
}
if (a[i] == '\0'&&b[j]=='\0')
return 1;
return 0;
}
int main()
{
char a[10000],ch[100];
int i,j,k=0,c=0;
cout<<"please input any centence you like best:"<<endl;
gets(a);
/* FILE *fp;
fp = fopen("c://lanqiao.txt","r");
fgets(a,sizeof(a),fp);
fclose(fp);*/
for (i=0; i<100; i++)
{ b[i].n = 1; }
for (i=0; i<=strlen(a); i++)
{
if ((a[i]==' '||a[i]==','||a[i] == '.'||a[i]=='\0') && k!=0)//不为英文
{
ch[k]='\0';
strcpy(b[c++].str, ch);//i have no reason to store a character without letter
k=0; //and k[0] always wait for some guys is not letter
}
else if (a[i]>='A'&&a[i]<='Z' || a[i]>='a'&&a[i]<='z')
{
ch[k++] = a[i];//all letter should store in and start k[1] and the like
}
}
for (i=0; i<c-1; i++)
{
if (b[i].str[0] != 0)
for (j=i+1; j<c; j++)
{
if (check(b[i].str, b[j].str)) //字符串不分大小写比较
{
b[i].n++;
b[j].str[0] = '\0';
}
}
}
for (i=0; i<c; i++)
{
if (b[i].str[0]!=0)
{
for (j=0; b[i].str[j]!='\0'; j++)
{
if (b[i].str[j]<='z'&&b[i].str[j]>='a')
{ b[i].str[j] -= 32; } //unify all word to be capital styles
cout<<b[i].str[j];
}
cout<<':';
for (j=0; j<b[i].n; j++)
{
cout<<'*';
}
cout<<b[i].n<<endl;
}
}
return 0;
}
输入一个由若干单词组成的文本行,每个单词之间用若干个空格隔开,统计此文本中单词的个数。/验证实验/
于 2018-06-25 09:12:08 首次发布