题目要求如下:
本题要求编写程序,从给定字符串中查找某指定的字符。
输入格式:
输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串(不超过80个字符)。
输出格式:
如果找到,在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标(下标从0开始);否则输出"Not Found"。
思路:使用getchar依次输入字符,与指定字符之间进行比较,用index的值来判断是否存在指定字符。
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
int index=-1,i=-1;
char s,ch;
scanf("%c\n",&s);
while((ch=getchar())!=EOF)
{
i++;
if(ch==s)
index=i;
}
if(index==-1)
printf("Not Found");
else
printf("index = %d",index);
return 0;
}