翻轉句子中單詞的順序(10)

第10題
翻轉句子中單詞的順序。
題目:輸入一個英文句子,翻轉句子中單詞的順序,但單詞內字符的順序不變。

句子中單詞以空格符隔開。爲簡單起見,標點符號和普通字母一樣處理。
例如輸入“I am a student.”,則輸出“student. a am I”。

 

 

/*
  Name: 
  Copyright: 
  Author: 
  Date: 30-08-11 19:26
  Description: 
*/
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
  //假設輸入的句子長度。
  char s[512];
  gets(s);
  int i,j,k;
  for(i=j=strlen(s)-1;j>=0;){
      if(s[j]==' '){
            for(i=j;i>=0;--i){
                if(s[i]!=' ')
		   break;   
            }
            if(i==-1)
		i=0;
            else
		i++;  
      }
      else{
          for(i=j;i>=0;--i){
              if(s[i]==' ')
	      break;
          }
          if(-1==i)
	     i=0;
          else
	     i++;  
      }
     for(k=i;k<=j;k++)
          cout<<s[k];
      j=i-1;
  }

  system("pause");
  return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章