2017年12月7日 星期四

[C_OT06-易] 英文句子字之倒轉(C++)

[C_OT06-易] 英文句子字之倒轉

題目說明:
請利用遞迴的方式撰寫一個程式,從檔案 in.txt 讀進一段沒有標點符號的英文文字,倒轉這段文字中字的順序,並輸出到一個文字檔 out.txt 。例如 in.txt 文字檔案內容為 This is a book and that is an apple ,則輸出檔 out.txt 為 apple an is that and book a is This 。
輸入說明:
輸入一個文字檔案 in.txt ,內容為一段沒有標點符號的英文字。
資料意義
第一筆英文字母組成的英文句子要被倒轉字的順序之英文句子
輸出說明 :
將讀進的英文文字,倒轉其順序並輸出到另一個文字檔 out.txt 。
輸入範例 :
This is a book and that is an apple
輸出範例 :
apple an is that and book a is This

說明:其實不用讀檔,當普通的input output就可以了。


  1. #include<iostream>  
  2. #include<string>  
  3. #include<sstream>  
  4.   
  5. using namespace std;  
  6.   
  7. int main(){  
  8.     // [C_OT06-易] 英文句子字之倒轉  
  9.     string text;  
  10.     getline(cin, text);  
  11.     string str_text[100000];  
  12.     int count = 0;  
  13.     stringstream ss(text);  
  14.     string token;  
  15.     while (getline(ss, token, ' '))  
  16.     {  
  17.         str_text[count] = token;  
  18.         count++;  
  19.     }  
  20.     for(int i = count-1;i >= 0;i--)  
  21.     {  
  22.         if(i == 0)  
  23.         {  
  24.             cout << str_text[i] << endl;  
  25.         }  
  26.         else  
  27.         {  
  28.             cout << str_text[i] << " ";  
  29.         }  
  30.     }  
  31.     return 0;  
  32. }  

沒有留言:

張貼留言