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

代码为:

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
相关推荐
Stack Overflow?Tan902 分钟前
linux ubuntu22.04安装ROS2humble完整版的流程
linux·docker·ros2
zly350010 分钟前
centos7 sshd无法启动
linux·运维·服务器
编程大师哥1 小时前
Linux 命名管道(FIFO)通信 超清晰讲解
linux·运维·服务器
Smile_2542204181 小时前
linux服务器清理磁盘
linux·运维·服务器
KivenMitnick2 小时前
Claude Code--Ubuntu Linux超详细配置教程(附每步的可能报错及解决方法)
linux·运维·ubuntu
panamera122 小时前
linux下SPI、IIC、UART、CAN的编码
linux·运维·服务器
欲盖弥彰13142 小时前
linux设备驱动 -- RK3568 led驱动 (led子系统&设备树)
linux·led·嵌入式linux·led子系统
三万棵雪松3 小时前
【Linux 物联网网关主控系统-Linux主控部分(四)】
linux·物联网·嵌入式linux
returnthem3 小时前
虚拟机sda紧急处理扩容(used100%,无空间剩余,开不了机)
linux·运维·服务器
Sean‘3 小时前
Linux系统下安装Trivy
linux·运维·服务器