国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++實現翻轉單詞順序

2020-05-23 14:02:22
字體:
來源:轉載
供稿:網友

題目:輸入一個英文句子,翻轉句子中單詞的順序,但單詞內字符的順序不變。句子中單詞以空格符隔開。為簡單起見,標點符號和普通字母一樣處理。例如輸入“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種方法了,希望小伙伴們能夠喜歡


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霞浦县| 东莞市| 信宜市| 福安市| 仁化县| 惠州市| 五寨县| 三明市| 兰溪市| 塘沽区| 晴隆县| 吉林省| 常州市| 新化县| 景洪市| 大港区| 方城县| 子长县| 祥云县| 全州县| 武陟县| 武山县| 淮安市| 高阳县| 崇州市| 通辽市| 宝丰县| 泸水县| 乐平市| 湛江市| 和政县| 长治市| 曲沃县| 盘山县| 呼图壁县| 廊坊市| 桐庐县| 青阳县| 葵青区| 佳木斯市| 晋江市|