C语言 将两个字符串连接起来,不用strcat函数

编一个程序,将两个字符串连接起来,不要用strcat函数。

cpp 复制代码
#include <stdio.h>

void my_strcat(char *s1, const char *s2) {
    while (*s1) {
        s1++;
    }
    while (*s2) {
        *s1 = *s2;
        s1++;
        s2++;
    }
    *s1 = '\0';
}

int main() {
    char s1[100] = "Hello, ";
    char s2[] = "World!";
    my_strcat(s1, s2);
    printf("连接后的字符串:%s\n", s1);
    return 0;
}

代码说明:

  • 将两个字符串连接起来,且不使用标准库中的`strcat`函数。

  • 通过遍历第一个字符串找到其结束位置,然后逐个复制第二个字符串的字符到第一个字符串末尾,最后添加结束符`'\0'`。

相关推荐
aaaameliaaa6 小时前
字符函数和字符串函数
c语言·笔记·算法
夜月yeyue6 小时前
AUTOSAR CP 从上电到 Runnable
c语言·网络·tcp/ip·车载系统
城管不管7 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯7 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡7 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯8 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang8 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9858 小时前
高进度算法模板速记(待完善)
java·前端·算法
小羊先生car8 小时前
RTOS-F429-HAL-绝对延时和相对延时(2026/7/31)
c语言·rtos
小王C语言8 小时前
【7. 实现登录注册模块】:实现会话管理、注册/登录/退出 API
网络·c++