10-1. 在字串中查詢指定字元(15)
阿新 • • 發佈:2019-02-07
輸入一個字串S,再輸入一個字元c,要求在字串S中查詢字元c。如果找不到則輸出“Not found”;若找到則輸出字串S中從c開始的所有字元。
輸入格式:
輸入在第1行中給出一個不超過80個字元長度的、以回車結束的非空字串;在第2行中給出一個字元。
輸出格式:
在一行中按照題目要求輸出結果。
輸入樣例1:It is a black box b輸出樣例1:
black box輸入樣例2:
It is a black box B輸出樣例2:
Not found
程式:
#include #define STRING_SIZE 80 int main(void) { char str[STRING_SIZE + 1]; char ch; int i = 0, flag = 0; gets(str); scanf("%c", &ch); while(str[i] != '\0') { if(str[i] == ch) { flag = 1; break; } ++i; } if(flag) { while(str[i] != '\0') { printf("%c", str[i]); ++i; } printf("\n"); } else printf("Not found\n"); return 0; }