#include<stdio.h>
#include<string.h>
#define MAX 80
char *s_gets(char *st,int n); //给ST字符串输入一组字符,不含换行符
char *strchr_ch(char *st,char ch); //对比第二个参数知道它在第一个参数中第一次出现的位置
int main(void)
{
char sr[MAX]; //声明字符串数组
char ch; //声明第二个参数为字符
char *st; //声明接受函数strchr_ch()函数返回值的字符串数组
printf(“Please enter a set of strings with no more than 80 characters:\n”);
s_gets(sr,MAX); //调用函数
puts(“Please enter the characters you need to find:”);
while(scanf("%c",&ch)) //函数strchr_ch()返回值为NULL的情况下,提示用户重新输入第二参数,可循环
{
st=strchr_ch(sr,ch); //函数返回值赋值给st
if(st==NULL){ //如果返回值是NULL重新输入反之结束循环
puts(“Sorry, no characters were found. Please re-enter:”);
scanf("%c",&ch);
}
else
break;
}
printf(“The character %c first appears in the string at the %d bit.\n”,
ch,(st-sr+1)); //打印第二参数在第一参数中的位置(第几个数组元素)
return 0;
}
char *s_gets(char *st,int n)
{
char *ret_val;
ret_val=fgets(st,n,stdin);
if(ret_val)
{
while(*st!=’\n’&&*st!=’\0’)
st++;
if(*st==’\n’)
*st=’\0’;
else
while(getchar()!=’\n’)
continue;
}
return ret_val;
}
char *strchr_ch(char *st,char ch)
{
int i=0;
char *aaa;
for(i=0;i<strlen(st);i++) //第一参数的长度为循环结束条件
{
if(st[i]==ch){
aaa=st+i;
return aaa;
}
}
return NULL;
}