Advertisement
fueanta

Total words on a sentence..!

Jun 12th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. int totalWordsOn(string sentence)
  2.     {
  3.         int counter = 0;
  4.         bool wordStarted = false;
  5.         for (int i = 0; i < sentence.size(); ++i)
  6.         {
  7.             int ascii = int(sentence[i]);
  8.             // A - Z = 65 - 90, a - z = 97 - 122
  9.             if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122))
  10.             {
  11.                 if (wordStarted == false)
  12.                 {
  13.                     counter++;
  14.                     wordStarted = true;
  15.                 }
  16.             }
  17.             else
  18.             {
  19.                 if (wordStarted == true)
  20.                 {
  21.                     wordStarted = false;
  22.                 }
  23.             }
  24.         }
  25.         return counter;
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement