统计good

题目源地址

Description

我们每天都面临着要学习大量的英文单词,但我最喜欢good这个词,这个词见到的越多,我的生活就越幸福和愉快!给你一些英文单词,你能统计出good有多少个吗?

Input

输入数据的第1行一个N (1 < N < 100),代表输入的数据组数,然后有N 行,每行有很多个单词,单词之间用空格隔开;每行的长度不超过100.

Output

对于每行数据,输出其中含good单词的个数。

Sample Input

2
i am a good boy!
goof good good dood good

Sample Output

1
3

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9   char a[100];
10   char b[100];
11   int n,k,count,j,i;
12   while (cin>>n)
13   {getchar();
14       while (n--)
15       {
16           count=0;
17           gets(a);
18 
19           k=strlen(a);
20 
21           for (i=0;i<k;i++)
22           {
23               if (a[i]!=' ')
24               {
25                   b[j]=a[i];
26                   j++;
27               }
28               else
29               {
30                   b[j]='\0';
31                   j=0;
32                   if (strcmp(b,"good")==0)
33                     count++;
34               }
35           }
36           if (a[k-1]=='d'&&a[k-2]=='o'&&a[k-3]=='o'&&a[k-4]=='g')
37             count++;
38           cout<<count<<endl;
39 
40       }
41   }
42     return 0;
43 }

PS: 在输入N之后,必须加上getchar!因为输入N之后回车键进入gets(),但是gets()后面不加getchar!这是为什么?最后的good字符没有了空格,所以要单独讨论。 当B中封上一次边儿之后必须让J回归零! 统计字符串中单词的个数和拆分单词(单词间不仅有一个空格——)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9   char a[100],b[100][100];
10   int i,j,m,n,num,k;
11   int flag;  
12   while (cin>>n)
13   {
14       while (n--)
15       {
16       getchar();
17       gets(a);
18       getchar();
19       m=strlen(a);
20       flag=0;j=0;num=0;k=0;
21       for (i=0;i<m;i++)
22       {
23           if (a[i]==' ')
24           {
25               if (flag==1)
26               {
27 
28                   b[k][j]='\0';
29                   k++;
30               }
31               flag=0;
32           }
33           else
34           {
35              if (flag==0)
36              {
37                  flag=1;
38                  num++;
39                  j=0;
40              }
41              b[k][j]=a[i];
42           }
43       }
44       b[k][j]='\0';
45       for (i=0;i<num;i++)
46         printf ("%s",b[i]);
47 
48 
49 
50 
51   }
52     return 0;
53 }
54 }

猜你喜欢

转载自www.cnblogs.com/twomeng/p/9475930.html
今日推荐