解决问题的时间首先想到的是搜索引擎,并不是大脑。现在的习惯都变完了!! //此段代码以解决以下方式的逆序 //原字符串: who is your name; //目标字符: ;eman ruoy si ohw #include <iostream> using namespace std; #define Max 200 int main() { char str[Max]; printf("请输入文本:"); gets(str); int len=0; char *strlen=str; char *left=str; char temp; while(*strlen++) { len++; } strlen-=2; while(left!=strlen) { temp=*left; *left++=*strlen; *strlen--=temp; } printf("逆序后的字符串为:"); for(int i=0;i<len;i++) { printf("%c",str[i]); } printf("/n"); system("pause"); return 0; } //此段代码是以按单词来逆序 #include <iostream> #include <assert.h> using namespace std; #define Max 200 char* ReversedText(char *originalText) { int textLength = 0; int wordCount = 0; int wordlength = 0; char reversingText[Max]; int i = 0; int j = 0; assert(originalText != NULL); textLength = strlen(originalText); reversingText[textLength] = '/0'; printf("输入文本:/"%s/"/n", originalText); while (i < textLength) { for (j = i; (j < textLength) && (isalnum(originalText[j]) || ('-' == originalText[j])); j++) { //blank block } wordlength = j-i; if (wordlength > 0) { memcpy(reversingText + textLength - j, originalText + i,wordlength); wordCount++; wordlength = 0; } else { //Blank block } if (j < textLength) { reversingText[textLength - j - 1] = originalText[j]; } else { break; } i = j+1; } reversingText[textLength] = '/0'; memcpy(originalText, reversingText, textLength); printf("逆序文本:/"%s/"/n",originalText); return originalText; } int main() { char str[Max]; printf("请输入文本:"); gets(str); ReversedText(str); printf("/n"); system("pause"); return 0; } #include "stdio.h" #include <iostream> using namespace std; //原:"What is your name+" //目:"name+ your is What" int main(void) { char s[]="What is your name+" ; char *a = NULL ; int l = strlen(s) - 1 ; while( l-- >= 0 ) { if ( *(s+l) == ' ' || !l ) { a = s + l + ( ( l ) ? ( 1 ) : ( 0 ) ) ; while ( *a && *a != ' ' ) putchar( *a++ ) ; putchar(' ') ; } } printf("/n"); system("pause"); return 0 ; } 以上代码段在VS2008下测试通过.