输入一个由若干单词组成的文本行,每个单词之间用若干个空格隔开,统计此文本中单词的个数。/验证实验/

 #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;  
}  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愿你温暖喜悦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值