|
发表于 2022-8-22 00:15:18
|
显示全部楼层
这是我统计苹果个数写的程序,可以看下:
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char a[128];
- char b[5]={'a','p','p','l','e'};
- int i,j,len,temp,count=0;
- char ch;
-
- printf("请输入一些水果:\n");
- for (i=0;(ch=getchar())!='\n';i++) //输入一些水果名称
- {
- a[i]=ch;
- }
- a[i]='\0';
- printf("您输入的这些水果是:%s\n",a); //打印输入的这些水果
- len=strlen(a);
- printf("这些水果字符的长度是:%d\n",len);
-
- for (i=0;i<len;i++) //把输入的水果的这些字符与"applp"相比较,一样的计数就加1
- {
- temp=i;
- for(j=0;j<5;j++)
- {
- if(a[temp]==b[j])
- {
- temp++;
- }
- else
- {
- break;
- }
-
- }
- if (j==5) count++;
-
- }
- printf("您输入的水果中一共有%d个苹果!\n",count); //打印输入苹果的个数
-
- return 0;
- }
复制代码
运行的结果如下:
- 请输入一些水果:
- apple peach banana apple apple apple pear
- 您输入的这些水果是:apple peach banana apple apple apple pear
- 这些水果字符的长度是:41
- 您输入的水果中一共有4个苹果!
- --------------------------------
- Process exited after 23.54 seconds with return value 0
- 请按任意键继续. . .
复制代码 |
|