我思故我在系列—数据结构10题(题目搜集整理者V_JULY_V,非常感谢!!)

我相信世界上最坚不可摧的力量就是相信自己的意志,敢于瞄准远大目标的决心,以及坚定追寻梦想的信心。

                                                                      ——摘自奥格.曼狄诺《羊皮卷》

第10 题

翻转句子中单词的顺序。
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

例如输入“I am a student.”,则输出“student. a am I”。


解法1:

reverse the whole sentence first,then reverse every word! take it as a good job!

#include <iostream>
#include<assert.h>
#include<string.h>
using namespace std;
/*reverse every string*/
void Reverse(char* begin, char* end)
{
    assert(begin!=NULL && end!=NULL);
    char tmp;
    while (begin < end)
         {
        tmp = *begin;
        *begin = *end;
        *end = tmp;
        begin ++;
        end --;
        }
}

char* ReverseSentence(char* sentence)
{
    assert(sentence!=NULL);
    char *begin, *end;
    begin = sentence;
    end = sentence;
    while (*end != '\0')
        end ++;
    end --;
    /*reverse the whole sentence*/
    Reverse(begin, end);

    /*reverse every string*/
    while (*begin != '\0')
          {
        end = begin;
        while (*end != ' ' && *end != '\0')
            end ++;
        end --;
        Reverse(begin, end);

        if (*(end + 1) == ' ')
                        {
                 begin = end + 2;
                        }
        else
                      {
               begin = end + 1;
                     }
         }
    return sentence;
}

***************************************************************************************************

解法2:

int main()
{
    int i=0;
    char* str[10]={0};
    char  str2[]= "I am a student.";
    char* p=NULL;
    str[0]=strtok(str2," ");         /*strtok的用法;*/
    while((p=strtok(NULL," "))!=NULL && str2!= NULL)
    {
       i++;
       str[i]=p;
    }
    for(int j=0,k=i;j<k;j++,k--)
    {
        char* tmp;
        tmp=str[j];
        str[j]=str[k];
        str[k]=tmp;
    }

    for(int k=0;k<(sizeof(str)/sizeof(str[0]));k++)
    {
        cout<<str[k]<<" ";
    }
    return 1;
}


******************************************************************************************************************************

解法3:逻辑性比较强;

class ReverseWords
{
 public:
    ReverseWords(string* wo):words(wo){}
    void reverse_();
 private:
   void reverse__(int begin,int end) ;
   string* words;
};

void ReverseWords::reverse__(int begin,int end)
{
    while(begin<end)
    {
    char t=words->at(begin);
    words->at(begin)=words->at(end);
    words->at(end)=t;
    ++begin;
    --end;
    }
}

void ReverseWords::reverse_()
{
     void reverse_();
       {
       int length=words->size();
       int begin=-1,end=-1;
       for(int i=0;i<length;++i)
           {
        if(begin==-1&&words->at(i)==' ')
           continue;
        if(begin==-1)
                 {
           begin=i;
           continue;
                  }
        if(words->at(i)==' ')
           end=i-1;
        else if(i==length-1)
           end=i;
        else
           continue;
       reverse__(begin,end);
       begin=-1,end=-1;
            }
      reverse__(0,length-1);
      }
}
int main()
{
  string s="I am a student.";
  ReverseWords r(&s);
  r.reverse_();
  cout<<s<<endl;
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值