2017年11月5日 星期日

[C_ST31-易] 移除字串中的空白鍵和定位鍵(C++)

**如果看不懂可以在底下留言發問**

**試著看懂別人的程式碼也是一種學習^ ^**

[C_ST31-易] 移除字串中的空白鍵和定位鍵

問題描述 :
給予一字串,其中包括英文大小寫字母、定位鍵與空白鍵。英文單字是以空白鍵分隔開。請刪除所有定位鍵與空白鍵後輸出只包含大小寫字母的英文字串。
輸入說明 :
輸入為長度在128個字母內的字串。

輸出說明 :
刪除所有定位鍵與空白鍵後的字串。

範例 :

Sample Input:Sample Output:
Clothes make the man. Don't judge a book by its cover.Clothesmaketheman.Don'tjudgeabookbyitscover.



  1. #include <iostream>  
  2. #include <string.h> //strcpy()   
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     // [C_ST31-易] 移除字串中的空白鍵和定位鍵  
  7.     string text;  
  8.     getline(cin, text);//使用者輸入(整行讀取包含空白)    
  9.     int count = text.size();//string 長度        
  10.      char result[count]; //存入結果        
  11.      strcpy(result, text.c_str());//string to char   
  12.      for(int i = 0;i < count;i++)    
  13.      {  
  14.         if(result[i] != 32 && result[i] != 9)  //不是空白也不是Tab
  15.         {  
  16.             if(i == count-1)  
  17.             {  
  18.                 cout << result[i] << endl;  
  19.             }  
  20.             else  
  21.             {  
  22.                 cout << result[i];  
  23.             }  
  24.         }  
  25.      }  
  26.     return 0;  
  27. }  

沒有留言:

張貼留言