題目:
5.設計并測試?個函數,搜索第1個函數形參指定的字符串,在其中查找第2個函數形參指定的字符?次出現的位置。如果成功,該函數返指向該字符的指針,如果在字符串中未找到指定字符,則返回空指針(該函數的功能與strchr()函數相同)。在?個完整的程序中測試該函數,使??個循環給函數提供輸?值。
6.編寫?個名為is_within()的函數,接受?個字符和?個指向字符串的指針作為兩個函數形參。如果指定字符在字符串中,該函數返回?個?零值(即為真)。否則,返回0(即為假)。在?個完整的程序中測試該函數,使??個循環給函數提供輸?值。
分析:
第5題和第6題類似,主要區別在于返回值不同,其中第6題需要函數返回值類似布爾值,第5題的函數返回值在如果找到字符的情況下返回字符指針,否則返回NULL空指針,同時第6題可以找到官方編程練習答案,第六題貼官方答案,第5題貼自己編寫的答案(有問題)以及AI優化后的答案。
思路:
需要用到教程中的s_gets()函數用于字符串的輸入,同時用scanf()函數或getchar()函數用于單個字符的輸入,用教程中的類似的指針遞增逐個字符比較的辦法( *str == ch, str++ );
第5題代碼(自編)
#include <stdio.h>
#include <string.h>
#define MAXLEN 20
char * charsrc(char * str, char ch); //按照題目要求建立函數
char * s_gets(char * str, int n); //測試程序用的用于獲取測試字符串的函數int main()
{char source_string[MAXLEN]; //測試用字符串char search_ch; //目標字符變量char * charsrc_result; //接住函數返回的指針puts("Please input a string."); //測試用字符串輸入if (s_gets(source_string, MAXLEN) == NULL){puts("Please re-input a string for test.");s_gets(source_string, MAXLEN);}puts("Please input character you want to search.");while (scanf("%c", &search_ch)){if ((charsrc_result = charsrc(source_string, search_ch)) != NULL){printf("Find the character %c inside string.\n", search_ch);printf("The pt of character is %p \n", &charsrc_result);puts(charsrc_result);break; }if ((charsrc_result = charsrc(source_string, search_ch)) == NULL){puts("Can't find the character inside the string.");break;}}return 0;
}char * s_gets(char * st, int n)
{char * ret_val;int i = 0;ret_val = fgets(st, n, stdin); //調用fgets讀取字符串if(ret_val) // 僅當fgets成功讀取時執行{// 找到換行符或字符串結束符的位置while (st[i] != '\n' && st[i] != '\0')i++;// 情況1:如果找到換行符,將其替換為字符串結束符\0if (st[i] == '\n')st[i] = '\0';// 情況2:如果沒找到換行符(說明輸入過長,緩沖區有殘留)else// 清空輸入緩沖區中剩余的字符(直到讀到換行符)while (getchar() != '\n')continue;}
}char * charsrc(char * str, char ch) //在一個字符串中搜索一個字符的函數
{while (*str != '\0'){str++;if (*str != '\0'){if (*str == ch){return str;break;}}else if (*str == '\0'){return NULL;break;}}
}
第5題代碼,AI 優化后
#include <stdio.h>
#include <string.h>
#define MAXLEN 20char * charsrc(char * str, char ch); // 查找字符的函數
char * s_gets(char * str, int n); // 獲取字符串的函數int main()
{char source_string[MAXLEN]; // 測試用字符串char search_ch; // 目標字符變量char * charsrc_result; // 函數返回的指針puts("Please input a string."); // 輸入測試字符串if (s_gets(source_string, MAXLEN) == NULL){puts("Input error!");return 1;}puts("Please input character you want to search.");if (scanf("%c", &search_ch) != 1) // 讀取目標字符{puts("Input error!");return 1;}// 清理輸入緩沖區中的換行符while (getchar() != '\n')continue;charsrc_result = charsrc(source_string, search_ch);if (charsrc_result != NULL){printf("Find the character '%c' in the string.\n", search_ch);printf("The address of the character is %p \n", (void *)charsrc_result);printf("The substring starting from this character: %s\n", charsrc_result);}else{printf("Can't find the character '%c' in the string.\n", search_ch);}return 0;
}// 讀取字符串,處理換行符和緩沖區
char * s_gets(char * st, int n)
{char * ret_val;int i = 0;ret_val = fgets(st, n, stdin);if(ret_val) // 僅當fgets成功讀取時執行{// 找到換行符或字符串結束符的位置while (st[i] != '\n' && st[i] != '\0')i++;// 如果找到換行符,將其替換為字符串結束符if (st[i] == '\n')st[i] = '\0';// 如果沒找到換行符,清空輸入緩沖區elsewhile (getchar() != '\n')continue;}return ret_val; // 增加返回值
}// 在字符串中搜索字符,返回首次出現的位置指針
char * charsrc(char * str, char ch)
{// 遍歷字符串,包括第一個字符while (*str != '\0'){if (*str == ch){return str; // 找到字符,返回當前指針}str++; // 移動到下一個字符}return NULL; // 未找到字符
}
第6題代碼,原書答案
#include <stdio.h>
#include <string.h>
#define LEN 80
_Bool is_within(const char *str, char c);
char * s_gets(char * st, int n);int main()
{char input[LEN];char ch;int found;puts("Enter a string:");while(s_gets(input, LEN) && input[0] != '\0'){puts("Enter a character:");ch = getchar();while (getchar() != '\n')continue;found = is_within(input, ch);if (found == 0)printf("%c not found in string.\n", ch);elseprintf("%c found in string %s\n",ch, input);puts("Next string:");}puts("Done.\n");return 0;
}char * s_gets(char * st, int n)
{char * ret_val;char * find;ret_val = fgets(st, n, stdin);if(ret_val){find = strchr(st, '\n');if(find)*find = '\0';elsewhile (getchar() != '\n')continue;}return ret_val;
}_Bool is_within(const char * str, char ch)
{while (*str != ch && *str != '\0') str++;return *str; //如果沒找到字符,*str == '\0'指向字符串末尾,返回0,否則返回非0值
}