From d2fe45558921dde8bde0e3adb016f5ad08f58414 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 22 Jul 2023 10:36:56 +0800 Subject: [PATCH] =?UTF-8?q?day=2011=20homework=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day11/homework/h4.c | 55 ++++++++++++++++++++++++++++++++ day11/homework/h8.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 day11/homework/h4.c create mode 100644 day11/homework/h8.c diff --git a/day11/homework/h4.c b/day11/homework/h4.c new file mode 100644 index 0000000..615ff8a --- /dev/null +++ b/day11/homework/h4.c @@ -0,0 +1,55 @@ +// 编写一个程序,要求用户输入一串字符,然后动态创建一个字符数组,并将用户输入的字符存储到数组中。然后,使用realloc函数将数组的大小扩展为原来的两倍,并继续接受用户输入的字符,直到用户输入字符为换行符( '\n')。最后,打印数组中存储的所有字符。 +#include +#include +#include + +char * +str_cpy(char *dest, const char *src) +{ + int cnt; + while (src[cnt++]) + ; + + char *res = (char *)malloc(cnt * sizeof(char *)); + if (NULL == res) + { + perror("malloc"); + exit(0); + } + + for (int i = 0; i < cnt; i++) + res[i] = src[i]; + + return res; +} + +int main() +{ + char s1[100]; // 相当于 char *s1 = (char *)malloc(100 * sizeof(char *)); + printf("请输入一个字符串: "); + // scanf("%s", s1); + fgets(s1, sizeof(s1), stdin); // scanf() 不能接收换行,故改用 fgets() + char *s2 = str_cpy(s2, s1); + + int tmp_size = strlen(s2); + + // 扩展数组大小 + s2 = (char *)realloc(s2, 2 * tmp_size); + if (NULL == s2) + { + perror("realloc"); + exit(0); + } + printf("请继续输入字符, 输入换行符结束输入\n"); + char *ch; + s2[tmp_size - 1] = ' '; + while ((ch = getchar()) != '\n') + { + s2[tmp_size++] = ch; + } + + printf("结果为: %s\n", s2); + free(s2); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h8.c b/day11/homework/h8.c new file mode 100644 index 0000000..297fa70 --- /dev/null +++ b/day11/homework/h8.c @@ -0,0 +1,78 @@ +// 设计函数 char (*tok(char *s, const char *delim))[100] ,实现s按给定的分隔符分隔出所有的内容,并返回分隔之后的二维数组指针。 +// 示例: +// char s[] = "Hello,World,How,Are,You"; +// const char delim[] = ","; +// char(*tokens)[100] = tok(s, delim); + +#include +#include +#include + +char (*tok(char *s, const char *delim))[100] +{ + // 计算分隔符的长度 + size_t delim_len = strlen(delim); + + // 计算字符串中分隔符的个数 + int count = 1; + char *p = s; + while (*p != '\0') + { + if (strncmp(p, delim, delim_len) == 0) + { + count++; + p += delim_len; // 跳过分隔符 + } + else + { + p++; + } + } + + // 分配存储结果的二维数组 + char(*tokens)[100] = malloc(count * sizeof(*tokens)); + + // 分隔字符串 + int i = 0; + p = s; + char *token_start = p; + while (*p != '\0') + { + if (strncmp(p, delim, delim_len) == 0) + { + strncpy(tokens[i], token_start, p - token_start); + tokens[i][p - token_start] = '\0'; + i++; + p += delim_len; // 跳过分隔符 + token_start = p; + } + else + { + p++; + } + } + + // 处理最后一个分隔符后的部分 + strncpy(tokens[i], token_start, p - token_start); + tokens[i][p - token_start] = '\0'; + + return tokens; +} + +int main() +{ + char s[] = "Hello,World,How,Are,You"; + const char delim[] = ","; + char(*tokens)[100] = tok(s, delim); + + // 打印分隔后的结果 + for (int i = 0; tokens[i][0] != '\0'; i++) + { + printf("%s\n", tokens[i]); + } + + // 释放内存 + free(tokens); + + return 0; +} \ No newline at end of file