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種方法了,希望小伙伴們能夠喜歡
相關(guān)文章
C語(yǔ)言中strcmp的實(shí)現(xiàn)原型
這篇文章主要介紹了C語(yǔ)言中strcmp的實(shí)現(xiàn)原型的相關(guān)資料,這里提供實(shí)例幫助大家理解這部分內(nèi)容,希望能幫助到大家,需要的朋友可以參考下2017-08-08
C語(yǔ)言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn)
這篇文章主要介紹了C語(yǔ)言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
ON_COMMAND_RANGE多個(gè)按鈕響應(yīng)一個(gè)函數(shù)的解決方法
這篇文章主要介紹了ON_COMMAND_RANGE多個(gè)按鈕響應(yīng)一個(gè)函數(shù)的解決方法,需要的朋友可以參考下2014-07-07
C++通過(guò)文件指針獲取文件大小的方法實(shí)現(xiàn)
本文主要介紹了C++通過(guò)文件指針獲取文件大小的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C語(yǔ)言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù)
貝塞爾曲線,又稱貝茲曲線或貝濟(jì)埃曲線,是應(yīng)用于二維圖形應(yīng)用程序的數(shù)學(xué)曲線。本文將利用C語(yǔ)言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù),需要的可以參考一下2022-12-12
C++中實(shí)現(xiàn)矩陣的加法和乘法實(shí)例
這篇文章主要介紹了C++中實(shí)現(xiàn)矩陣的加法和乘法實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
使用C++和代理IP實(shí)現(xiàn)天氣預(yù)報(bào)的采集
在當(dāng)今的互聯(lián)網(wǎng)時(shí)代,網(wǎng)絡(luò)信息的獲取變得日益重要,天氣預(yù)報(bào)信息作為日常生活的重要參考,其獲取方式也隨著技術(shù)的發(fā)展而不斷變化,在本文中,我們將探討如何使用C++和代理IP來(lái)采集天氣預(yù)報(bào)信息,文中通過(guò)代碼講解的非常詳細(xì),需要的朋友可以參考下2023-12-12

