Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. int strLen(char* str)
  2. {
  3.     int size = 0;
  4.  
  5.     for (int i = 0; str[i] != '\0'; i++)
  6.     {
  7.         size++;
  8.     }
  9.  
  10.     return size;
  11. }
  12.  
  13. long getWords(char str[])
  14. {
  15.     long count = 0;
  16.     long strLength = strLen(str);
  17.  
  18.     if (strLength == 0)
  19.         return -1;
  20.  
  21.     for (int i = 0; i <= strLen(str); i++)
  22.     {
  23.         if ((str[i] == ' ' || str[i] == '\t') && i == 0)
  24.             continue;
  25.  
  26.         if ((str[i] == ' ' || str[i] == '\t' || str[i] == '\0') && str[i - 1] != ' ' && str[i - 1] != '\t')
  27.         {
  28.             count++;
  29.         }
  30.     }
  31.  
  32.     return count;
  33.    
  34. }
  35.  
  36. int main()
  37. {
  38.     char words[100]{ 0 };
  39.  
  40.     std::cin.getline(words, 100);
  41.  
  42.     std::cout << getWords(words);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement