C++实现翻转单词顺序

时间:2021-05-02

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student. a am I”。

思路:首先将整个句子按字符翻转,然后再将其中每个单词的字符旋转。

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include <string> #include "stdafx.h" void Reverse(char *pBegin, char *pEnd) { if(pBegin == NULL || pEnd == NULL) return; while(pBegin < pEnd) { char temp = *pBegin; *pBegin = *pEnd; *pEnd = temp; pBegin ++, pEnd --; } } char* ReverseSentence(char *pData) { if(pData == NULL) return NULL; char *pBegin = pData; char *pEnd = pData; while(*pEnd != '\0') pEnd ++; pEnd--; // 翻转整个句子 Reverse(pBegin, pEnd); // 翻转句子中的每个单词 pBegin = pEnd = pData; while(*pBegin != '\0') { if(*pBegin == ' ') { pBegin ++; pEnd ++; } else if(*pEnd == ' ' || *pEnd == '\0') { Reverse(pBegin, --pEnd); pBegin = ++pEnd; } else { pEnd ++; } } return pData; } int main() { char input[] = "I am a student."; printf("%s\n\n",input); printf("After reverse.\n\n"); ReverseSentence(input); printf("%s\n", input); return 0; }

再给大家分享一段一位国外网友的实现方法

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <stdio.h> #include <string.h> int main() { char str[50001], ch; int i, low, high, tmp, len; while( gets( str ) ) { low = 0; high = 0; len = strlen( str ); while( low < len ) { while( str[low] == ' ' ) { low++; } high = low; while( str[high] ) { if( str[high] == ' ' ) { high--; break; } else { high++; } } if( str[high] == '\0' ) { high--; } tmp = high + 1; while( low < high ) { ch = str[low]; str[low] = str[high]; str[high] = ch; low++; high--; } low = tmp; high = tmp; } for( i = len - 1; i > 0; i-- ) { printf("%c", str[i]); } printf("%c\n", str[0]); } return 0; }

再来一个小编的代码

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 #include <iostream> using namespace std; void reverse_part(char*,int pBegin,int pEnd); void reverse(char *str) { //n为字符串长度 int n=strlen(str)-1; reverse_part(str,0,n); int pBegin=0,pEnd=0; while(str[pEnd+1]){ if(str[pEnd]!=' ' && str[pEnd]!='\0') ++pEnd; //找到空格 else{ reverse_part(str,pBegin,pEnd-1); //如果下一个还是空格 while(str[pEnd+1]!='\0' && str[pEnd+1]==' ') ++pEnd; pBegin=++pEnd; } } cout<<str<<endl; } void reverse_part(char *str,int pBegin,int pEnd) { char temp; for(int i=pBegin;i<=(pEnd-pBegin)/2;++i){ temp=str[i]; str[i]=str[pEnd-i]; str[pEnd-i]=temp; } } void main() { char str[]="I am a student."; reverse(str); system("pause"); } #include <iostream> using namespace std; void reverse_part(char*,int pBegin,int pEnd); void reverse(char *str) { //n为字符串长度 int n=strlen(str)-1; reverse_part(str,0,n); int pBegin=0,pEnd=0; while(str[pEnd+1]){ if(str[pEnd]!=' ' && str[pEnd]!='\0') ++pEnd; //找到空格 else{ reverse_part(str,pBegin,pEnd-1); //如果下一个还是空格 while(str[pEnd+1]!='\0' && str[pEnd+1]==' ') ++pEnd; pBegin=++pEnd; } } cout<<str<<endl; } void reverse_part(char *str,int pBegin,int pEnd) { char temp; for(int i=pBegin;i<=(pEnd-pBegin)/2;++i){ temp=str[i]; str[i]=str[pEnd-i]; str[pEnd-i]=temp; } } void main() { char str[]="I am a student."; reverse(str); system("pause"); }

以上就是解决单词顺序翻转的3种方法了,希望小伙伴们能够喜欢

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章