Advertisement
Felanpro

Character Counter (Tutorial From Youtuber)

Nov 3rd, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /*PROBLEM STATEMENT CHALLENGE (FROM YOUTUBER)*/
  2.    
  3.     //Ask the user to input a word while the word length < 5
  4.     string word = "";
  5.    
  6.     wordCounterMachine:
  7.     do{
  8.     cout << "Enter a word that has atleast 5 characters: " << endl;
  9.     cin >> word;
  10.       }while(word.size() < 5);
  11.    
  12.     //Ask the user to input a character
  13.     char searchChar = '0';
  14.    
  15.     cout << "Enter a character and the program will tell you how many times it appears in the word " << word << "." << endl;
  16.     cin >> searchChar;
  17.    
  18.     int counter = 0;
  19.     //Iterate over the word
  20.     for(int i = 0; i < word.size(); i++)
  21.     {
  22.  
  23.     //Get a character
  24.     char ch = word.at(i);
  25.    
  26.     //If the character matches the character we're looking for
  27.     if(searchChar == ch)
  28.     {
  29.    
  30.     //Increment a counter
  31.     counter++;            //counter = counter + 1;
  32.     }
  33.     }
  34.    
  35.     //Output the number of times the character was found in the word
  36.     cout << "The number of " << searchChar << "'s in the word " << word << " is " << counter << ".\n" << endl;
  37.     system("pause");
  38.     goto wordCounterMachine;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement