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 != '\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;
}
再給大家分享一段一位國外網友的實現方法
#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");
}
以上就是解決單詞順序翻轉的3種方法了,希望小伙伴們能夠喜歡
上一篇:深度理解c++中的this指針
欄 目:C語言
下一篇:C/C++: Inline function, calloc 對比 malloc
本文標題:C++實現翻轉單詞順序
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2190.html
您可能感興趣的文章
- 04-02c語言沒有round函數 round c語言
- 01-10數據結構課程設計-用棧實現表達式求值的方法詳解
- 01-10使用OpenGL實現3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關鍵字含義
- 01-10求斐波那契(Fibonacci)數列通項的七種實現方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數字運算符做加法
- 01-10使用C++實現全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現DBSCAN聚類算法
- 01-10深入全排列算法及其實現方法


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-11ajax實現頁面的局部加載
- 01-10C#中split用法實例總結
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數量限制代碼修改


