int main11()//用for实现 { char *a="wqwqdadadadqeadafdsfewqew"; char *b="wq"; int i; int icount=0; // a=strstr(a,"wq"); for(;*a!=' ';) { //if(strstr(a,"wq")!=NULL)//这句不对的原因就是strstr自动查找的 a=strstr(a,"wq");//按这里来说strstr是自己自动查找的 这里如果不匹配返回NULL if(a!=NULL) { icount++; a=a+strlen("wq"); } else//没有这个break还会致错! break; } printf("%d ",icount); system("pause"); }
int main22()//用while实现 { char *a="wqwqdadadadqeadafdsfewqew"; char *b="wq"; int i; int icount=0; a=strstr(a,"wq"); while(a!=' ') { a=strstr(a,"wq"); if(a!=NULL) { icount++; a=a+strlen("wq"); } else//没有这个break还会致错! break; } printf("%d ",icount); a="wqwqdadadadqeadafdsfewqew"; icount=0; while (a=strstr(a,b))//整个循环中直到a中不会再出现b的序列,则返回NULL { icount++; a=a+strlen(b); /*if(*a ==' ') 因此这步就是多余的 break;*/ } printf("%d ",icount); system("pause"); }
int main33()//用 do while实现 { char *a="wqwqdadadadqeadafdsfewqew"; char *b="wq"; int i; int icount=0; do { icount++; a=a+strlen(b); }while(a=strstr(a,"wq")); printf("%d ",icount); system("pause"); }
//实现接口 int showT(char **mya,char ** myb) { char **Tmya=mya; char **Tmyb=myb; int icount=0; while( *(Tmya)=strstr(*Tmya,*Tmyb) ) { icount++; *Tmya=*Tmya+strlen(*Tmyb); } return icount; } int main() { char *a="wqwqdadadadqeadafdsfewqew"; char *b="wq"; int i=0; i=showT(&a,&b); printf("%d ",i); system("pause"); }
int shouT2(char *mya,char *myb,int *myicount) { char *Tmya=mya; char *Tmyb=myb; int *Tmyicount=myicount; *myicount=0; while( Tmya=strstr(Tmya,Tmyb) ) { (*myicount)++;//这里导致错误,因为++ 和* 的优先级不同,++的优先级比*的高 Tmya=Tmya+strlen(Tmyb); } return *myicount; } int main() { char *a="wqwqdadadadqeadafdsfewqew"; //char b[]="wq";这里的b出来的是乱码,因为没有字符终止符 char *b="wq"; int i=0; i=shouT2(a,b,&i); printf("%d ",i); system("pause"); }