百度了一下另外兩位同學的做法,都是先判斷是否匹配,然後再用一個for()循環輸出,我當然也是先判斷,然後,就直接puts(),還是巧妙一點,題設要求及代碼實現如下
/*
Name:
Copyright:
Author:
Date: 03/04/15 14:49
Description:
輸入一個字符串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 <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX 80
void print(char * S, int l, char c);
int main()
{
// freopen("in.txt", "r", stdin); // for test
char S[80], c;
int l;
gets(S);
l = strlen(S);
c = getchar();
print(S, l, c);
// fclose(stdin); // for test
return 0;
}
void print(char * S, int l, char c)
{
int i;
for(i = 0; i < l; i++)
{
if(S[i] == c)
{
puts(S + i);
break;
}
}
if(i == l)
printf("Not found\n");
}