1. 程式人生 > >用C語言實現字串倒序

用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);
}

執行結果:

[email protected]:/home/adams/c# ./2
hello adams 
smada olleh 
[email protected]:/home/adams/c#