急求!!!C语言编程。编写一个函数在字符数组中查找指定字符

2024-12-17 03:23:39
推荐回答(5个)
回答1:

#include

int find(char ch[], char data);
void main()
{
char ch[100];
char data;

printf("input ch[]:");
gets(ch);

printf("input data:");
data = getchar();

printf("%d\n", find(ch, data));

}
int find(char ch[], char data)
{
int i;

for (i = 0; ch[i] != '\n'; i++)
{
if (data == ch[i])
{
return (i);
}
}

return (-1);
}

回答2:

int find( char *words , int length , char word){
//*words用来存放数组的首地址,word 为要查找的字符,length为数组长度

int flag = -1, index = 0; //index为数组的下标,flag 为标志

for(index = 0; index < length; index++){
if(words[index] != word){
}else{ //可以节约判断的次数,减小时空复杂度
flag = index;
break;
}
}
return flag;
}

回答3:

int find(char *a,char b)
{
for(int i=1;*a!='\0';a++,i++)
if(*a==b)
return i;
return -1;
}

回答4:

那个2级的写的有点错,应该是return(i+1)

回答5:

int find(char *s, int length, char tobefound){
int i, j;
j = -1;
for(i = 0; i < length; ++i){
if(s[i] == tobefound){
j = i;
break;
}
}
return j;
}