翻转句子中单词的顺序

本文介绍了一个用于翻转英文句子中单词顺序的算法实现,包括颠倒整个句子字符顺序和翻转每个单词内字符顺序的过程。提供了解决方案的C代码示例,并讨论了不同情况下的优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

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

分析:由于编写字符串相关代码能够反映程序员的编程能力和编程习惯,与字符串相关的问题一直是程序员笔试、面试题的热门题目。本题也曾多次受到包括微软在内的大量公司的青睐。

由于本题需要翻转句子,我们先颠倒句子中的所有字符。这时,不但翻转了句子中单词的顺序,而且单词内字符也被翻转了。我们再颠倒每个单词内的字符。由于单词内的字符被翻转两次,因此顺序仍然和输入时的顺序保持一致。

还是以上面的输入为例子。翻转“I am a student.”中所有字符得到“.tneduts a ma I”,再翻转每个单词中字符的顺序得到“students. a am I”,正是符合要求的输出。

//reverse the single word
int ReverseString(char *pBegin, char *pEnd) {
    if (pBegin == NULL || pEnd == NULL) {   
        return -1; 
    }
    //begin reverse
    while (pBegin < pEnd) {
        char temp = *pBegin;
        *pBegin = *pEnd;   
        *pEnd = temp;  
        pBegin ++, pEnd --;  
    }

    return 0;
}

//reverse the whole string sentence
int ReverseSentence(char *pStr) {
    if (NULL == pStr) {
        return -1;
    }
    char *pBegin = pStr; 
    char *pEnd = pStr + strlen(pStr) - 1; 
    
    //reverse the whole string first 
    ReverseString(pBegin, pEnd);    
    
    //then, from the whole reverse string begin
    //we reverse the single word which bewteen the two '.'    
    pBegin = pStr;    
    pEnd   = pStr;
    while ('\0' != *pBegin) {  
        pEnd = strchr(pBegin, '.'); 
        if (NULL != pEnd) {
            ReverseString(pBegin, -- pEnd);
            ++ pEnd;
        }
        else {     
            pEnd = pBegin + strlen(pBegin) - 1;   
            ReverseString(pBegin, pEnd); 
        } 
        pBegin = ++ pEnd; 
    }
    return 0;
}


参考代码:

#include "stdio.h"

///////////////////////////////////////////////////////////////////////
// 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;
}
int main()
{
	char pData[] = "I am a student.";
	ReverseSentence(pData);
	printf("%s\n",pData);
}

以上 转自:http://zhedahht.blog.163.com/blog/static/254111742007289205219/,在此表示感谢!

我的原创改进型的方法

一)不考虑字符串开头结尾有空格和单词之间有多个空格:

#include <stdio.h>
#include <string.h>

void Reverse(char *pBegin, char *pEnd)  
{  
      if(NULL == pBegin || NULL == pEnd )  
            return;  
  
      while(pBegin < pEnd)  
      {  
            char temp = *pBegin;  
            *pBegin = *pEnd;  
            *pEnd = temp;  
  
            pBegin ++, pEnd --;  
      }  
}  
char* ReverseSentence(char *pData)  
{  
      if(NULL == pData)  
            return NULL;  
  
      char *pBegin = pData;  
      char *pEnd = pData;  
  
      while('\0' != *pEnd)  
            pEnd ++;  
      pEnd --;  
  
      // Reverse the whole sentence   
      Reverse(pBegin, pEnd);  
  
      // Reverse every word in the sentence   
      pBegin = pEnd = pData;  

      while('\0' != *pBegin)  
      {  
		  pEnd = strstr(pBegin," ");

		  //first,we must be if the pEnd is NULL or not
		  if(NULL != pEnd)
		  {
			  Reverse(pBegin, -- pEnd);			  
			  ++ pEnd;			  
		  }
		  else
		  {
			  pEnd = pBegin + strlen(pBegin) - 1;			 
			  Reverse(pBegin, pEnd);			  
		  }
		  pBegin = ++ pEnd;
      }  
  
      return pData;  
}  

int main()  
{  
    char pData[] = "I am a student. thank you very much";  
    ReverseSentence(pData);  
    printf("%s\n",pData);  
}  

二)考虑字符串开头结尾有空格和单词之间有多个空格:

#include <stdio.h>
#include <string.h>

void Reverse(char *pBegin, char *pEnd)  
{  
      if(NULL == pBegin || NULL == pEnd )  
            return;  
  
      while(pBegin < pEnd)  
      {  
            char temp = *pBegin;  
            *pBegin = *pEnd;  
            *pEnd = temp;  
  
            pBegin ++, pEnd --;  
      }  
}  
char* ReverseSentence(char *pData)  
{  
      if(NULL == pData)  
            return NULL;  
  
      char *pBegin = pData;  
      char *pEnd = pData + strlen(pData) - 1;  
  
      //while('\0' != *pEnd)  
      //      pEnd ++;  
      //pEnd --;  
  
	  //think of possible appear ' ' at the begin of the strings
	  while(' ' == *pBegin)
		  pBegin ++;

	  //think of possible appear ' ' at the end of the strings
	  while(' ' == *pEnd)
		  pEnd --;
      // Reverse the whole sentence   
      Reverse(pBegin, pEnd);  
  
      // Reverse every word in the sentence   
      pBegin = pEnd = pData;  

      while('\0' != *pBegin)  
      {  
		  pEnd = strstr(pBegin," ");

		  //first,we must be if the pEnd is NULL or not
		  if(NULL != pEnd)
		  {
			  Reverse(pBegin, -- pEnd);	
			  pEnd ++;
			  while(' ' == *pEnd)
				  pEnd ++;
			  pBegin = pEnd;
		  }
		  else
		  {
			  pEnd = pBegin + strlen(pBegin) - 1;			 
			  Reverse(pBegin, pEnd);
			  pBegin = ++ pEnd;
		  }
		  
      }  
  
      return pData;  
}  

int main()  
{  
    char pData[] = " I   am   a   student.  thank   you   very much  ";  
    ReverseSentence(pData);  
    printf("%s",pData);  
}  




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值