2016年12月21日 星期三

C 語言 字串反轉


#include <stdio.h>

void main(void)
{
    //字元陣列不用宣告大小, 結束字元必需加上 \0
    char str[] = {'A', 'B', 'C', 'D', 'E', 'F', '\0'};
    char temp[1];                   // 暫存字串

    int str_length = sizeof(str)-1; // 字元長度減去結束字元, 長度為6
    int count;

    for(count=0; count<(str_length/2); count++)
    {
        temp[0] = str[count];
        str[count] = str[str_length-1-count];
        str[str_length-1-count] = temp[0];
    }

    printf("Reverse:(%s)\n", str);
}

沒有留言:

張貼留言