淡泊明志 宁静致远
分类: C/C++
2006-11-25 15:57:31
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
#include
/*
Searches a string for a given character,
which may be the null character '\0'.
查找字符串string中首次出现字符ch的位置。如果string中存在字符ch,返回首次出现
*/
char * my_strchr(const char *str, int ch)
{
while (*str
&& *str != (char)ch)
str++;
if (*str == (char)ch)
return((char *)str);
return(NULL);
}
int main()
{
char *str =
"ammana_babi";
char * p;
char ch;
ch = '9';
p = (char
*)my_strchr(str,ch);
if(p == NULL)
printf("Can't find the character %c
!\n",ch);
else
printf("Find the character %c
!\n",*p);
ch = 'b';
p = (char
*)my_strchr(str,ch);
if(p == NULL)
printf("Can't find the character %c
!\n",ch);
else
printf("Find the character %c
!\n",*p);
system("pause");
return 0;
}