手机
当前位置:查字典教程网 >编程开发 >C语言 >C++实现翻转单词顺序
C++实现翻转单词顺序
摘要:题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输...

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

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

#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 != '') pEnd ++; pEnd--; // 翻转整个句子 Reverse(pBegin, pEnd); // 翻转句子中的每个单词 pBegin = pEnd = pData; while(*pBegin != '') { if(*pBegin == ' ') { pBegin ++; pEnd ++; } else if(*pEnd == ' ' || *pEnd == '') { Reverse(pBegin, --pEnd); pBegin = ++pEnd; } else { pEnd ++; } } return pData; } int main() { char input[] = "I am a student."; printf("%snn",input); printf("After reverse.nn"); ReverseSentence(input); printf("%sn", input); return 0; }

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

#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] == '' ) { 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("%cn", str[0]); } return 0; }

再来一个小编的代码

#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]!='') ++pEnd; //找到空格 else{ reverse_part(str,pBegin,pEnd-1); //如果下一个还是空格 while(str[pEnd+1]!='' && 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]!='') ++pEnd; //找到空格 else{ reverse_part(str,pBegin,pEnd-1); //如果下一个还是空格 while(str[pEnd+1]!='' && 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种方法了,希望小伙伴们能够喜欢

【C++实现翻转单词顺序】相关文章:

C/C++回调函数介绍

用C++实现一个链式栈的实例代码

c++ 巧开平方的实现代码

C++中函数的默认参数详细解析

基于C语言实现shell指令的详解

利用C++的基本算法实现十个数排序

C++中const的实现机制深入分析

C++实现strcmp字符串比较的深入探讨

C++中的对象数组详细解析

用c++实现将文本每个单词首字母转换为大写

精品推荐
分类导航