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

代码为:

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
相关推荐
margu_1686 小时前
现代 Linux 系统管理tmp目录临时文件的标准方式
linux·服务器
qiuyunoqy6 小时前
Linux进程 --- 5(进程地址空间初识)
linux·c++·算法
夜月yeyue6 小时前
Linux 文件设备类型分析
linux·运维·网络·单片机
IMPYLH6 小时前
Bash 的 basenc 命令
linux·运维·服务器·bash·shell
小黄人软件6 小时前
openclaw Windows安装 国内OK 解决安装过程中任何问题 linux mac等 申请AI免费的token 无法访问此页面
linux·windows·macos·openclaw
微露清风6 小时前
系统性学习Linux-第八讲-进程间通信
java·linux·学习
confiself6 小时前
deer-flow2本地启动(无make无nginx启动)
linux·人工智能
IMPYLH7 小时前
Linux 的 arch 命令
linux·运维·服务器·bash
吹牛不交税7 小时前
netcore项目发布部署到阿里云linux系统Alibaba Cloud Linux3的docker
linux·阿里云·docker
_Emma_7 小时前
【Raspberry PI】Raspberry PiSP摄像头前端(rpl-cfe)
linux·驱动开发