C语言 字符串逆置
将字符串逆置输出,如原字符串为“abcdefgh”,逆置后输出“hgfedcba”。
#include <stdio.h> int main() { //定义变量 int count=0,i=0; char str[]="abcdefgh"; char *s,*p,temp; //输出原字符串 printf("原字符串:%s\n",str); //头指针s s=str; //统计字符串长度 while(str[i++]) count++; //尾指针p p=s+count-1; //头指针指向的内容和尾指针指向的内容对调 do { temp=*s; *s=*p; *p=temp; s++; p--; }while(s<p); //输出逆置后字符串 printf("逆置后字符串:%s\n",str); return 0; }