2017年9月24日 星期日

[UVa]272 - TEX Quotes(C++)

第一個 (")變成 ( ``)
第二個 (")變成 ( '' )
範例如下
輸入 : "To be or not to be," quoth the bard, "that is the question."
輸出 : ``To be or not to be,'' quoth the bard, ``that is the question.''


  1. #include <iostream>   
  2. using namespace std;    
  3.   
  4. int main() {    
  5.     char c;    
  6.     int k = 0;    
  7.     while(cin.get(c))  //可以讀整行,遇到enter也繼續  
  8.     {    
  9.         if(c != '"')  //c不是(")就保持原樣  
  10.         {    
  11.             cout << c;  //輸出c  
  12.         }    
  13.         else if(++k % 2)  //如果是第一個(")  
  14.         {    
  15.             cout << "``";  //輸出(``)  
  16.         }    
  17.         else  //如果是第二個(")  
  18.         {    
  19.   
  20.             cout << "''";  //輸出('')  
  21.         }    
  22.     }    
  23.     return 0;    
  24. }  

沒有留言:

張貼留言