Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux C 學習:字串逆序輸出的小技巧

看了網上的字串逆序輸出,大多相對複雜,下面給一個簡單的字串逆序輸出小程式:

[cpp] view
plain copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
int n;
char *q = "I am a coder";
n = strlen(q);
char buffer[n+1];//多一個位置放 '\0'
printf("The string is:%s\n",q);
for( i = 0;i < n;i++)
{
buffer[n-1-i]=*q++;
}
buffer[n]='\0';
printf("The ending string is:%s\n",buffer);
return 0;
}

運行結果如下:

Linux C 學習:字串逆序輸出的小技巧

Copyright © Linux教程網 All Rights Reserved