Advertisement
TheWhiteFang

Tutorial 4 Section A (b) [count and print]

Nov 16th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int count(const string& input) //function defined to count the blank spaces
  8.  
  9. {
  10.     int spaces = 0;
  11.  
  12.     for (int i = 0; i < input.size(); i++){
  13.         if (input[i] == ' '){ //we can access each character in a string
  14.             ++spaces;
  15.         }
  16.     }
  17.     return spaces;
  18. }
  19.  
  20.  
  21. int main(){
  22.  
  23.     string name = " ";
  24.  
  25.  
  26.  
  27.     cout << "Please enter a string:\n";
  28.     getline(cin, name);
  29.     cout << endl;
  30.  
  31.     cout <<"Your entered string is: "<< name <<endl <<endl;//partA
  32.    
  33.     int countspace = count(name);
  34.    
  35.     cout << "Number of white-space characters" << " is " << countspace << "\n\n";
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.     return 0;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement