程序员面试题精选(07)-翻转句子中单词的顺序

程序员面试题精选(07)-翻转句子中单词的顺序
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入I am a student.,则输出student. a am I
分析:由于编写字符串相关代码能够反映程序员的编程能力和编程习惯,与字符串相关的问题一直是程序员笔试、面试题的热门题目。本题也曾多次受到包括微软在内的大量公司的青睐。
由于本题需要翻转句子,我们先颠倒句子中的所有字符。这时,不但翻转了句子中单词的顺序,而且单词内字符也被翻转了。我们再颠倒每个单词内的字符。由于单词内的字符被翻转两次,因此顺序仍然和输入时的顺序保持一致。
还是以上面的输入为例子。翻转I am a student.中所有字符得到.tneduts a ma I,再翻转每个单词中字符的顺序得到students. a am I,正是符合要求的输出。
参考代码:

下面是C++版本的,之后我还会写个C#版本的。
///
// Reverse a string between two pointers
// Input: pBegin - the begin pointer in a string
//
        pEnd   - the end pointer in a string
///
void Reverse(
char *pBegin, char *pEnd)
{
      if(pBegin == NULL || pEnd == NULL)
            return;
      while(pBegin < pEnd)
      {
            char temp = *pBegin;
            *pBegin = *pEnd;
            *pEnd = temp;
            pBegin ++, pEnd --;
      }
}
///
// Reverse the word order in a sentence, but maintain the character
// order inside a word
// Input: pData - the sentence to be reversed
///
char* ReverseSentence(
char *pData)
{
      if(pData == NULL)
            return NULL;

      char *pBegin = pData;
      char *pEnd = pData;
      while(*pEnd != '/0')
            pEnd ++;
      pEnd--;

      // Reverse the whole sentence
      Reverse(pBegin, pEnd);

      // Reverse every word in the sentence
      pBegin = pEnd = pData;
      while(*pBegin != '/0')
      {
            if(*pBegin == ' ')
            {
                  pBegin ++;
                  pEnd ++;
                  continue;
            }
            // A word is between with pBegin and pEnd, reverse it
            else if(*pEnd == ' ' || *pEnd == '/0')
            {
                  Reverse(pBegin, --pEnd);
                  pBegin = ++pEnd;
            }
            else
            {
                  pEnd ++;
            }
      }
      return pData;
}

C#版本的Code:

 

Reverse a string between two pointers
// Input: start- the begin index in a string
//          end- the end index in a string

//     strText- the string needs to be resversed.

string Reverse(int start, int end,string strText)
{
      if(start <0||end <0 ||start > end))
            return;
      while(start< end)
      {
            char temp = strText[start];
         strText[start] =strText[end];
             strText[end]=temp;
           start++;
 end- -;
      }

   Return strText;
}

// Reverse the word order in a sentence, but maintain the character
// order inside a word
// Input: strText- the sentence to be reversed

string ReverseSentence(string strText)

{

  if (String.IsNullOrEnpty(strText))

   {

    return;

   }

   int start =0;

   int length = strText.Length;

   int end = length-1;

   Reverse(start,end,strText);

   end = start;

   While(start<length)

   {

     if (strText[start]==" ")

     {

       start ++;

       end++;

       continue;
     }

     else if (strText[end]==""||strText[start] =='/0')

     {

       Reverse(start,end,strText);

       end++;

       start = end;

     }

     else

     {

       end ++;

     }

  

   } 

  Return strText;

   

}

 

 



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值