字符串函数参数传入传出(字符串反转)
/*** strstr.c ***/ #include<stdio.h> #include<string.h> //求字符串p中abcd出现的次数 //自定义函数接口完成业务函数和main函数分开 int getCount(char *mystr,char *sub,int *ncount) { int ret = 0; if(mystr == NULL || sub == NULL || ncount == NULL) { ret = -1; printf("one of point is NULL\n"); return ret; } int tmpCount = 0; char *p = mystr; //不要轻易改变形参的值 do { p = strstr(p,sub); if(p != NULL) { tmpCount++; p = p +strlen(sub); } else { break; } }while(*p != '\0'); *ncount = tmpCount; //间接赋值是指针存在的最大意义 return ret; } int main() { int ret = 0; char *p = "abcd11122abcd3333abcd3456abc"; int count = 0; char sub[] = "abcd"; ret = getCount(p,sub,&count); if(ret != 0) { printf("getCount error ret:%d\n",ret); return ret; } printf("count:%d\n",count); return 0; }