Advertisement
XuanHong

LTTS - đếm số từ trong câu (xóa khoảng trắng)

Jan 28th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. // LTTS - đếm số từ trong câu (xóa khoảng trắng)
  2.  
  3. #include <iostream>
  4. #include <string >
  5. using namespace std;
  6.  
  7. string s;
  8.  
  9. void main()
  10. {
  11.     getline(cin, s);
  12.     // xóa khoảng trắng ở đầu
  13.     while (s[0] == ' ')
  14.     {
  15.         s.erase(s.begin());
  16.     }
  17.     // xóa khoảng trắng ở cuối
  18.     while (s[s.size() - 1] == ' ')
  19.     {
  20.          s.erase(s.end()-1);
  21.     }
  22.     // xóa khoảng trắng ở giữa
  23.     while (s.find("  ")<s.size())
  24.     {
  25.         s.erase(s.find("  "),1);
  26.     }
  27.  
  28.     int k = 0;
  29.    
  30.     for (int i = 0; i < s.size(); i++)
  31.     {
  32.         if (s[i] == ' ')
  33.         {
  34.             k++;
  35.         }
  36.     }
  37.     cout << k+1 <<endl;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement