C++实现翻转单词顺序

所属分类: 软件编程 / C 语言 阅读数: 59
收藏 0 赞 0 分享

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多