编写一个函数 char *my_strnchr(char const *str,int ch,int which)

来源:互联网 发布:万宝瑞华人才管理 知乎 编辑:程序博客网 时间:2024/05/16 05:41
/**************************************** *  File Name  : my_strnchr.c *  Creat Data : 2015.1.22*  Author     : ZY *****************************************/ /*编写一个函数char *my_strnchr(char const *str,int ch,int which)第三个参数指定ch字符在str字符串中第几次出现。例如,如果第三个参数为1,这个函数的功能就和strchr完全一样。如果第三个参数为2,这个函数就返回一个指向ch字符在str字符串中第二次出现的位置的指针。*/#include <stdio.h>#include <assert.h>char *my_strnchr(char const *str,int ch,int which){int count = 0;assert(str);while(*str){if( ch == *str ){count++;if( count == which ){return str;}}*str++;}return NULL;}int main()  {  char *arr="123452";int a = '2';int count = 1;if( NULL == my_strnchr(arr,a,count) )  {  printf("Can't find the character %c !\n",a);  }  else  {  printf("Find the character %c !\n",a);  printf("%p\n",my_strnchr(arr,a,count));  }  return 0;  }  

0 0
原创粉丝点击