// 把student a am i 变成 i am a student //思想:把每个单词翻转,然后再把整体翻转 #include <stdio.h> #include <string.h> //把每次解析出来的单词翻转 void fanw( char *l,char *r ) { char* left = l; char* right = r; char temp; while( left < right ) { temp = *left; *left = *right; *right = temp; left++; right--; } } //解析出来每个单词 void fans( char *p ) { while( *p != '\0') { char *pst = p; while( *p != '\0' && *p != ' ' ) { p++; } fanw( pst,p-1 ); p++; } } int main() { char p[30] = "student a am i"; int len = strlen(p); printf("原字符串是:%s\n",p); printf("翻转后的字符串是:"); fanw(p,p+len-1); fans(p); printf("%s\n",p); return 0; }
时间: 2024-10-26 13:28:46