翻转

请你编写函数实现按单词反转字符串,所谓的按单词反转字符串并不是简单的字符串反转,而是按给定字符串里的单词将字符串倒转过来,就是说字符串里面的单词还是保持原来的顺序,这里的每个单词用空格分开。例如:Here is www.fishksy.com.cn经过反转后变为:www.fishksy.com.cn is Here

/******************************************************************************************************
  * @file name:		  :BubbleSort
  * @brief  		  :逆序输出
  * @author 		  :wvjnuhhail@126.com
  * @date 			  :2024/05/10
  * @version 1.0 	  :V1.0
  * @property 		  :暂无
  * @note   		  :None
  * CopyRight (c)  2023-2024   wvjnuhhail@126.com   All Right Reseverd
******************************************************************************************************/
char* reverse_word(const char* str)
{  
   int len = strlen(str);
   
  char* restr = new char[len+1];
  
  strcpy(restr,str);  int i,j;
  
  for(i=0,j=len-1;i<j;i++,j--)  
{    char temp=restr[i]; 

	restr[i]=restr[j]; 
    
	restr[j]=temp;   }
	
	int k=0;   
	
		while(k<len)   
		{    
  	 		i=j=k;
    			while(restr[j]!=' ' && restr[j]!='' )       
   			j++;  
            
  			 k=j+1; 
  			 
   			j--;  
            
			for(;i<j;i++,j--)    
			{      
  				char temp=restr[i];   
  				 restr[i]=restr[j];   
   				restr[j]=temp;  
  			} 
 }  
return restr;
}
posted @ 2024-05-10 20:33  WJnuHhail  阅读(7)  评论(0编辑  收藏  举报