C primer plus 第六版 第十一章 第六題 程式設計練習答案
阿新 • • 發佈:2018-12-28
Github地址: φ(>ω<*)這裡這裡。
/*
本次任務跟任務5(11章程式設計練習5)基本相同,不過這次只要返回一個非零值好了。。
ps:其實這種題目都會了,我能不能改一個函式名就算完成了╮(╯﹏╰)╭
*/
#include<stdio.h> #define o 100 int is_within(char chr, char * orig); void get(char * get); int main(void) { int place1 = 0; char orig[o] = {}; char find = 0; 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 a true value.\n\n"); // 英文不過小學水平不要介意。。大概意思知道就行。 while( quit != 'q' ) { printf("Now, Please input string:\n"); get(orig); printf("\nNow, Please input character: "); scanf("%c", &find); fflush(stdin); place1 = is_within(find, orig); if( place1 != 0 ) { printf("Find that character, return subscript %d. Do you want to quit " "or try again ?(Enter 'q' to quit) :",place1); scanf("%c", &quit); fflush(stdin); } else { printf("We don't find it, return 0. Do you want to quit or try again ?(Enter 'q' to quit) : "); scanf("%c", &quit); fflush(stdin); } } printf("\nBye ~\n"); getchar(); return 0; } int is_within(char chr, char * orig) { int i = 0; // 這個變數在下面的程式碼中並沒有任何卵用。只是。。 // 要返回一個非零值,我就返回指定字元在字串中的下標好了。。。。 int place2 = 0; while(*orig != '\n' && *orig != EOF) { place2 = (*orig == chr ? i : 0 ); if( place2 == 0) { orig++; i++; } else { break; } } return place2; } void get(char * get) { while( ( *get = getchar() ) != '\n' ) { get++; } fflush(stdin); return; }