C++實(shí)現(xiàn)翻轉(zhuǎn)單詞順序
題目:輸入一個(gè)英文句子,翻轉(zhuǎn)句子中單詞的順序,但單詞內(nèi)字符的順序不變。句子中單詞以空格符隔開(kāi)。為簡(jiǎn)單起見(jiàn),標(biāo)點(diǎn)符號(hào)和普通字母一樣處理。例如輸入“I am a student.”,則輸出“student. a am I”。
思路:首先將整個(gè)句子按字符翻轉(zhuǎn),然后再將其中每個(gè)單詞的字符旋轉(zhuǎn)。
#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--; // 翻轉(zhuǎn)整個(gè)句子 Reverse(pBegin, pEnd); // 翻轉(zhuǎn)句子中的每個(gè)單詞 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; }
再給大家分享一段一位國(guó)外網(wǎng)友的實(shí)現(xiàn)方法
#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; }
再來(lái)一個(gè)小編的代碼
#include <iostream> using namespace std; void reverse_part(char*,int pBegin,int pEnd); void reverse(char *str) { //n為字符串長(zhǎng)度 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); //如果下一個(gè)還是空格 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為字符串長(zhǎng)度 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); //如果下一個(gè)還是空格 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"); }
以上就是解決單詞順序翻轉(zhuǎn)的3種方法了,希望小伙伴們能夠喜歡
上一篇:深度理解c++中的this指針
欄 目:C語(yǔ)言
下一篇:C/C++: Inline function, calloc 對(duì)比 malloc
本文標(biāo)題:C++實(shí)現(xiàn)翻轉(zhuǎn)單詞順序
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2190.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改