C primer plus 第六版 第十一章 第五題 程式設計練習答案
阿新 • • 發佈:2018-12-28
Github地址:φ(>ω<*)這裡這裡。
/*
本次任務需設計並測試一個函式,
該函式接受兩個引數,通過第二個引數接受一個指定字串,並在第一個引數中尋找其首次出現的位置。
如果未找到則返回空指標。 使用一個迴圈給函式提供輸入值。
ps: 這個能不能偷懶直接用strchr()啊。。。。。。。。ヾ(o・ω・)ノ
*/
#include<stdio.h> #define o 100 char * strfind(char * orig, char chr); void get(char * get); int main(void) { char orig[o] = {}; char find = 0; char * place1 = NULL; char quit = 0; printf("Please input two arguments. \nThe first is a string and second is a character. \n" "The second argument is search in first argument," " and return its exist place of first .\n\n"); // 英文不過小學水平不要介意。。大概意思知道就行。 while( quit != 'q' ) { printf("Now, Please input string:\n"); get(orig); printf("\nNow, Please input character: "); scanf("%c", &find); fflush(stdin); place1 = strfind(orig, find); if( place1 != NULL ) { printf("Find that character, do you want to quit or try again ?(Enter 'q' to quit) :"); scanf("%c", &quit); fflush(stdin); } else { printf("We don't find it. do you want to quit or try again ?(Enter 'q' to quit) : "); scanf("%c", &quit); fflush(stdin); } } printf("\nBye ~\n"); getchar(); return 0; } char * strfind(char * orig, char chr) { char * place2 = NULL; while(*orig != '\n' && *orig != EOF) { place2 = (*orig == chr ? orig : NULL ); if( place2 == NULL) { orig++; } else { break; } } return place2; } void get(char * get) { while( ( *get = getchar() ) != '\n' ) { get++; } fflush(stdin); return; }