求指定字符在字符串中第一次出现的位置并输出

2025-02-26 17:50:15
推荐回答(1个)
回答1:

#include
#include
void nono(int posi);
int fun(char str1[],char ch)
{
int i,pos;
/***********begin***********/
for(i=0;i {
if(str1[i] ==ch){
pos=i;
return pos;
}
}
return pos;

/************end************/
}
main()
{ char str1[80]="This Is a c Program",ch='a';
int posi;
printf("String is: %s\n",str1);
printf("char is: %c\n",ch);
posi=fun(str1,ch);
printf("Result is: %d\n",posi);
nono(posi);
}

void nono(int posi)
{FILE *f;
f=fopen("out1.dat","w");
fprintf(f,"The sum is :%d\n",posi);
fclose(f);
}