用C语言实现字符串倒序

程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fun(char *w)
{
        char t, *s1,*s2;
        int n = strlen(w);
        s1 = w;
        s2 = w+n-1;
        while(s1<s2)
        {
                t = *s1;
                *s1 = *s2;
                *s2 = t;
                s1++;
                s2--;
        }
}
int main()
{
        char w[] = "hello adams";
        printf("%s \n",w);
        fun(w);
        printf("%s \n",w);
}

运行结果:

root@ubuntu:/home/adams/c# ./2
hello adams 
smada olleh 
root@ubuntu:/home/adams/c# 

猜你喜欢

转载自blog.csdn.net/weixin_38184741/article/details/81073510