1. 程式人生 > >練習7-10 查詢指定字元 (15 point(s))

練習7-10 查詢指定字元 (15 point(s))

練習7-10 查詢指定字元 (15 point(s))

本題要求編寫程式,從給定字串中查詢某指定的字元。

輸入格式:

輸入的第一行是一個待查詢的字元。第二行是一個以回車結束的非空字串(不超過80個字元)。

輸出格式:

如果找到,在一行內按照格式“index = 下標”輸出該字元在字串中所對應的最大下標(下標從0開始);否則輸出"Not Found"。

輸入樣例1:

m
programming

輸出樣例1:

index = 7

輸入樣例2:

a
1234

輸出樣例2:

Not Found
#include<stdio.h>
int main(){
  
  int i,count=0,index=-1;
  char a,ch,str[80];
  scanf("%c\n",&a);
  ch=getchar();
  for(i=0;ch!='\n';i++){
    str[i]=ch;
    count++;
    ch=getchar();
  }
  for(i=0;i<count;i++){
    if(a==str[i])
    index=i;
  }
  if(index!=-1)
  printf("index = %d",index);
  else 
  printf("Not Found");
  return 0;
  
}