鱼C论坛

 找回密码
 立即注册
查看: 622|回复: 4

[已解决]用c实现strlen strcat 以及strcmp函数要求不使用指针表示法

[复制链接]
发表于 2024-9-12 21:03:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用c实现strlen   strcat  以及strcmp函数要求不使用指针表示法
最佳答案
2024-9-12 21:03:56
好的,鱼油!为了不使用指针表示法,我们可以使用数组下标来实现 `strlen`、`strcat` 和 `strcmp` 函数。以下是这三个函数的代码:

### 实现 `strlen`

  1. int my_strlen(const char s[]) {
  2.     int length = 0;
  3.     while (s[length] != '\0') {
  4.         length++;
  5.     }
  6.     return length;
  7. }
复制代码


### 实现 `strcat`

  1. void my_strcat(char dest[], const char src[]) {
  2.     int i = 0, j = 0;
  3.     // 找到dest末尾
  4.     while (dest[i] != '\0') {
  5.         i++;
  6.     }
  7.     // 追加src到dest末尾
  8.     while (src[j] != '\0') {
  9.         dest[i] = src[j];
  10.         i++;
  11.         j++;
  12.     }
  13.     dest[i] = '\0';
  14. }
复制代码


### 实现 `strcmp`

  1. int my_strcmp(const char s1[], const char s2[]) {
  2.     int i = 0;
  3.     while (s1[i] != '\0' && s2[i] != '\0') {
  4.         if (s1[i] != s2[i]) {
  5.             return s1[i] - s2[i];
  6.         }
  7.         i++;
  8.     }
  9.     return s1[i] - s2[i];
  10. }
复制代码


这三段代码分别实现了 `strlen`、`strcat` 和 `strcmp` 函数,并且没有使用指针表示法。希望这对你有帮助!如果有任何问题,请随时询问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-12 21:03:56 | 显示全部楼层    本楼为最佳答案   
好的,鱼油!为了不使用指针表示法,我们可以使用数组下标来实现 `strlen`、`strcat` 和 `strcmp` 函数。以下是这三个函数的代码:

### 实现 `strlen`

  1. int my_strlen(const char s[]) {
  2.     int length = 0;
  3.     while (s[length] != '\0') {
  4.         length++;
  5.     }
  6.     return length;
  7. }
复制代码


### 实现 `strcat`

  1. void my_strcat(char dest[], const char src[]) {
  2.     int i = 0, j = 0;
  3.     // 找到dest末尾
  4.     while (dest[i] != '\0') {
  5.         i++;
  6.     }
  7.     // 追加src到dest末尾
  8.     while (src[j] != '\0') {
  9.         dest[i] = src[j];
  10.         i++;
  11.         j++;
  12.     }
  13.     dest[i] = '\0';
  14. }
复制代码


### 实现 `strcmp`

  1. int my_strcmp(const char s1[], const char s2[]) {
  2.     int i = 0;
  3.     while (s1[i] != '\0' && s2[i] != '\0') {
  4.         if (s1[i] != s2[i]) {
  5.             return s1[i] - s2[i];
  6.         }
  7.         i++;
  8.     }
  9.     return s1[i] - s2[i];
  10. }
复制代码


这三段代码分别实现了 `strlen`、`strcat` 和 `strcmp` 函数,并且没有使用指针表示法。希望这对你有帮助!如果有任何问题,请随时询问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-9-12 21:42:49 | 显示全部楼层
  1. #include <stdio.h>

  2. size_t strlen_(const char *str) {
  3.     if(!*str) return 0;
  4.     return strlen_(str + 1) + 1;
  5. }

  6. int main(void) {
  7.     char buff[1024];
  8.     scanf("%s", buff);
  9.     printf("str: "%s", strlen: %zu\n", buff, strlen_(buff));
  10.     return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-9-12 21:43:58 | 显示全部楼层
  1. #include <stdio.h>

  2. size_t strlen_(const char str[]) {
  3.     if(!str[0]) return 0;
  4.     return strlen_(&str[1]) + 1;
  5. }

  6. int main(void) {
  7.     char buff[1024];
  8.     scanf("%s", buff);
  9.     printf("str: "%s", strlen: %zu\n", buff, strlen_(buff));
  10.     return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-9-12 21:53:06 | 显示全部楼层
  1. 反斜杠让吃了
复制代码


2024-09-12-214349_1920x1080_scrot.jpeg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 07:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表