c语言:返回指定字符在指定字符串中第n次出现的位置(从0开始计算)

来源:互联网 发布:ubuntu网卡 编辑:程序博客网 时间:2024/05/15 23:36
/*Function 返回指定字符在指定字符串中第n次出现的位置(从0开始计算)*//*函数原型 int myFunc(const char *s, char c, int n)*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>int myFunc(const char *s, char c, int n){    int flag = 0;    int index = 0;    if (NULL == s)    {        return -1;    }    while (*s != '\0')    {        if (*s == c)        {            ++flag;            ++index;            ++s;            if (flag == n)            {                return index;            }        }        ++s;        ++index;    }    return index;}int main(){    char *str = "welcome to you";    int n =2;    char ch = 'o';    int index = 0;    index = myFunc(str,ch,n);    printf("index = %d\n",index);    system("pause");    return 0;}
0 0
原创粉丝点击