【每日一函数】字符串反转

代码为:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void my_print(char *string)
{
    printf("The string is %s\n", string); /* 输出一个字符串 */
}

void my_print2(char *string)
{
    char *string2;
    int size, size2, i;
    size = strlen(string);
    size2 = size - 1;
    string2 = (char *)malloc(size + 1); /* 为string2分配存储空间 */
    for (i = 0; i < size; i++)          /* 将字符串string的内容复制到string2中 */
    {
        string2[size2 - i] = string[i];
    }
    string2[size] = '\0';                                   /* 添加'\0'结束符 */
    printf("The string printed backward is %s\n", string2); /* 输出字符串 */
}

int main(void)
{
    char my_string[] = "hello there"; /* 第一个字符串 */
    my_print(my_string);              /* 调用两个函数 */
    my_print2(my_string);
    return 0;
}

编译及执行:

bash 复制代码
➜  c git:(liucc) ✗ gcc -o string_reverse string_reverse.c
➜  c git:(liucc) ✗ ./string_reverse 
The string is hello there
The string printed backward is ereht olleh
相关推荐
程序设计实验室2 小时前
经历分享,发现挖矿木马后,服务器快速备份与重装(腾讯云平台)
linux
Miku163 小时前
OpenClaw-Linux+飞书官方Plugin安装指南
linux·人工智能·agent
Miku163 小时前
OpenClaw 接入 QQ Bot 完整实践指南
linux·人工智能·agent
Yogurt_cry8 小时前
[树莓派4B] 闲置近10年的爱普生 L310 打印机爆改无线打印机
linux·物联网·树莓派
Johny_Zhao1 天前
OpenClaw中级到高级教程
linux·人工智能·信息安全·kubernetes·云计算·yum源·系统运维·openclaw
Sheffield2 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield2 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
Johny_Zhao3 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev3 天前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw