code4sk/c/sourcecode/朱森森带领的奇妙冒险/字符串1/查找指定字符.cpp
2023-10-18 16:17:32 +08:00

48 lines
786 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//查找指定字符
//本题要求编写程序,从给定字符串中查找某指定的字符。
//输入格式:
//输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串不超过80个字符
//输出格式:
//如果找到在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标下标从0开始否则输出"Not Found"。
//输入样例1
//m
//programming
//输出样例1
//index = 7
//输入样例2
//a
//1234
//输出样例2
//Not Found
#include<stdio.h>
#include<string.h>
int main(){
int i,count=0,index;
char c;
scanf("%c\n",&c);
char str[81];
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==c)
{
index=i;
count++;
}
}
if(count!=0)
printf("index = %d\n",index);
else if(count==0)
printf("Not Found");
return 0;
}