. myAt函数,原型是int myAt(const char *s, char c, int n);功能是返回指定字符在指定字符串中第n次出现的位置(从0开始计算)
楼主请采纳,已解决!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int myAt(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 = myAt(str,ch,n);
printf("index = %d\n",index);
system("pause");
return 0;
}