这题的主要难点在于怎样判断一句话中是否存在chi1 huo3 guo1这串字符。
可以用string头文件中的strstr函数来判断。
strstr函数原型如下
char *strstr(const char *haystack, const char *needle);
实例
int main(void) { char str[] = "I welcome any ideas from readers, of course."; char *c1=strstr(str, "come"); printf("come:%sn",c1); char *c2=strstr(str, "icome"); printf("icome:%sn",c2); return 0; }
这里需要注意的是,因为 strstr 函数与 strpbrk 函数不同,strstr 函数匹配的是字符串,所以语句“strstr(str,"icome")”将返回 NULL。运行结果为:
come:come any ideas from readers, of course.
icome:(null)(摘自C语言中文网)
还有一点需要注意的是,当一行中只有字符串"."时,才停止输入,并且这一行不包括在内。
#include#include //包含strstr函数的头文件 int main(void) { int i,j,t; int count1=0,count2=0; char a[101][81]; for(int i=0;i<101;i++) { gets(a[i]); if(a[i][0]=='.'&&a[i][1]=='')//判断一行中是否只有"." break; else count1++;//count1用来计数不包含"."的信息行数 } printf("%dn",count1); for(int k=0;k 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)