[ C語言生活記事 ] - String 相關的 function (5) strrev

簡單實現C語言中有幾個string相關的function。

part 5 - strrev (string reverse)

Function Name : strrev

Definition : Reverse the input string.

Prototype : void strrev_imp ( char *str );

 

#include <stdio.h>
#include <string.h>

void strrev_imp ( char *str )
{   
    char temp;
    int i,j;
    
    for( i = 0, j = strlen(str) - 1 ; i < j ; ++i , --j )
    {
        temp = str[j];
        str[j] = str[i];
        str[i] = temp;
    }
}

int main()
{
    char s1[30] = "stringA";
    
    strrev_imp(s1);
    
    printf("result = %s\n", s1);

    return 0;
}

 

Result : 

sh-4.2$ gcc -o main *.c
sh-4.2$ main
result = Agnirts