C++實現(xiàn)翻轉(zhuǎn)單詞順序
題目:輸入一個英文句子,翻轉(zhuǎn)句子中單詞的順序,但單詞內(nèi)字符的順序不變。句子中單詞以空格符隔開。為簡單起見,標(biāo)點符號和普通字母一樣處理。例如輸入“I am a student.”,則輸出“student. a am I”。
思路:首先將整個句子按字符翻轉(zhuǎn),然后再將其中每個單詞的字符旋轉(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)整個句子 Reverse(pBegin, pEnd); // 翻轉(zhuǎn)句子中的每個單詞 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; }
再給大家分享一段一位國外網(wǎng)友的實現(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; }
再來一個小編的代碼
#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"); }
以上就是解決單詞順序翻轉(zhuǎn)的3種方法了,希望小伙伴們能夠喜歡
相關(guān)文章
C語言設(shè)計實現(xiàn)掃描器的自動機的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語言設(shè)計實現(xiàn)掃描器的自動機,可識別的單詞包括:關(guān)鍵字、界符、標(biāo)識符和常整型數(shù),感興趣的小伙伴可以了解一下2022-12-12C++分析構(gòu)造函數(shù)與析造函數(shù)的特點梳理
本文對類的構(gòu)造函數(shù)和析構(gòu)函數(shù)進行總結(jié),主要包括了構(gòu)造函數(shù)的初始化、重載、使用參數(shù)和默認(rèn)參數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),希望能幫助讀者在程序開發(fā)中更好的理解類,屬于C/C++基礎(chǔ)2022-05-05C++ COM編程之QueryInterface函數(shù)(一)
這篇文章主要介紹了C++ COM編程之QueryInterface函數(shù)(一),QueryInterface是組件本身提供對自己查詢的一個接口,需要的朋友可以參考下2014-10-10C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹
以下是對C++中的的sstream標(biāo)準(zhǔn)庫進行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-09-09