c++编程定义函数int FineCh(char *str , char ch)?

2025-03-24 10:22:54
推荐回答(2个)
回答1:

字符串查找1
@函数名称: strpbrk
函数原型: char *strpbrk(const char *s1, const char *s2)
函数功能: 在字符串s1中寻找字符串s2中任何一个字符相匹配的第一个字符的位置,空字符NULL不包括在内。
函数返回: 返回指向s1中第一个相匹配的字符的指针,如果没有匹配字符则返回空指针NULL。
参数说明:
所属文件: #include
#include
int main()
{
char *p="Find all vowels";while(p)
{
printf("%s\n",p);
p=strpbrk(p+1,"aeiouAEIOU");
}
return 0;
}字符串查找2
@函数名称: strcspn
函数原型: int strcspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
函数返回: 长度
参数说明:
所属文件: #include
#include int main()
{
printf("%d\n",strcspn("abcbcadef","cba"));
printf("%d\n",strcspn("xxxbcadef","cba"));
printf("%d\n",strcspn("123456789","cba"));
return 0;
}字符串查找3
@函数名称: strspn
函数原型: int strspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
函数返回: 位置指针
参数说明:
所属文件: #include
#include
#include
int main()
{
printf("%d\n",strspn("out to lunch","aeiou"));
printf("%d\n",strspn("out to lunch","xyz"));
return 0;
}字符串查找4
@函数名称: strchr
函数原型: char* strchr(char* str,char ch);
函数功能: 找出str指向的字符串中第一次出现字符ch的位置
函数返回: 返回指向该位置的指针,如找不到,则返回空指针
参数说明: str-待搜索的字符串,ch-查找的字符
所属文件: #include
#include
int main()
{
char string[15];
char *ptr, c='r';
strcpy(string, "This is a string");
ptr=strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n",c,ptr-string);
else
printf("The character was not found\n");
return 0;
}字符串查找5
@函数名称: strrchr
函数原型: char *strrchr(const char *s, int c)
函数功能: 得到字符串s中最后一个含有c字符的位置指针
函数返回: 位置指针
参数说明:
所属文件: #include
#include
int main()
{
char string[15];
char *ptr,c='r';
strcpy(string,"This is a string");
ptr=strrchr(string,c);
if (ptr)
printf("The character %c is at position:%d",c,ptr-string);
else
printf("The character was not found");
return 0;
}字符串查找6
@函数名称: strstr
函数原型: char* strstr(char* str1,char* str2);
函数功能: 找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回: 返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件: #include
#include
int main()
{
char *str1="Open Watcom C/C++",*str2="Watcom",*ptr;
ptr=strstr(str1,str2);
printf("The substring is:%s\n",ptr);
return 0;
}本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/tsyj810883979/archive/2010/01/01/5116817.aspx

回答2:

#include
using namespace std;

int FineCh(char *str , char ch)
{
char* a = str;
while(*str && *str != ch)
++str;
return *str ? str - a + 1 : 0;
}

int main()
{
cout << FineCh("1234641347", '6');
}