题目:
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值
}