对一个字符串回文输出,如“how are you doing”,回文后输出应该是“doing you are how”,注意,倒序的只是单词的顺序,但是每个单词内部的字母仍然是按照原来的顺序输出。程序如下:
#include <stdio.h> void main(void) { char arr[] = "how are you doing"; char *p, *p1, *pnext, *head; char temp; head = arr; p = arr; p1 = arr; while (*p1 != '\0') { p1++; } p1--; while (p1 > p) { temp = *p1; *p1 = *p; *p = temp; p++; p1--; } p = head; p1 = head; while (*p != '\0') { while ((*p1 != ' ')&&(*p1!='\0')) { p1++; } if (*p1 == '\0') { pnext = p1; } else { pnext = p1 + 1; } p1--; while (p1 > p) { temp = *p1; *p1 = *p; *p = temp; p1--; p++; } p = pnext; p1 = pnext; } printf("%s", head); }