实验7-3-2 查找指定字符(15 分)

实验7-3-2 查找指定字符(15 分)

本题要求编写程序,从给定字符串中查找某指定的字符。

输入格式:

输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串(不超过80个字符)。

输出格式:

如果找到,在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标(下标从0开始);否则输出”Not Found”。

输入样例1:

1
2
m
programming

输出样例1:

1
index = 7

输入样例2:

1
2
a
1234

输出样例2:

1
Not Found
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<string.h>
char buf[80],search[80];
int main(){

scanf("%s",search);
scanf("%s",buf);
char * pos = strstr(buf,search);
if (pos==NULL)
printf("Not Found\n");
else
printf("index = %d\n",pos-buf+1);
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×