最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)翻轉(zhuǎn)單詞順序

 更新時(shí)間:2016年07月03日 09:29:26   投稿:hebedich  
這篇文章給大家匯總介紹了C++實(shí)現(xiàn)翻轉(zhuǎn)單詞順序的三種方法,都非常的簡單,需要的朋友可以參考下

題目:輸入一個(gè)英文句子,翻轉(zhuǎn)句子中單詞的順序,但單詞內(nèi)字符的順序不變。句子中單詞以空格符隔開。為簡單起見,標(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;
}

再給大家分享一段一位國外網(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; 
}

再來一個(gè)小編的代碼

#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); 
      //如果下一個(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為字符串長度
 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)文章

最新評(píng)論

阿尔山市| 会同县| 成安县| 内乡县| 宜宾市| 嘉禾县| 龙岩市| 砀山县| 临汾市| 靖边县| 张掖市| 高邮市| 秦皇岛市| 临城县| 维西| 普宁市| 页游| 乐至县| 晋江市| 华阴市| 高安市| 土默特左旗| 泾阳县| 固镇县| 靖州| 叙永县| 怀仁县| 永定县| 泸水县| 沈阳市| 霍山县| 登封市| 镇康县| 玉环县| 定结县| 柯坪县| 朔州市| 四子王旗| 新建县| 翁源县| 监利县|