fork(2) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. size_t GetNumberOfWords(const char* text)
  5. {
  6. size_t count = 0;
  7.  
  8. while(*text)
  9. {
  10. //перематываем все пробельные символы
  11. while(*text && isspace(*text) ) ++text;
  12.  
  13. //если перемоталась вся строка - закончить работу
  14. if(!(*text) ) break;
  15.  
  16. //если мы здесь, значит мы наткнулись на "слово"
  17. //увеличиваем счетчик слов, и перематываем его
  18. ++count;
  19. while(*text && !isspace(*text) ) ++text;
  20. }
  21. return count;
  22. }
  23.  
  24. int main()
  25. {
  26. enum { eSIZE = 100};
  27. const char test1[eSIZE] = " Hello word hello word";
  28. const char test2[eSIZE] = "Hello word hello word";
  29. const char test3[eSIZE] = "Hello word hello word";
  30. const char test4[eSIZE] = "Hello word hello word ";
  31.  
  32. cout<<'\n';
  33. cout<<"WELCOME TO TEST OF FUNCTION size_t GetNumberOfWords(const char* text); \n";
  34.  
  35. cout<<"source text :"<<test1<<endl;
  36. cout<<"number of words in the text = "<< GetNumberOfWords(test1)<<endl<<endl;
  37.  
  38. cout<<"source text :"<<test2<<endl;
  39. cout<<"number of words in the text = "<< GetNumberOfWords(test2)<<endl<<endl;
  40.  
  41. cout<<"source text :"<<test3<<endl;
  42. cout<<"number of words in the text = "<< GetNumberOfWords(test3)<<endl<<endl;
  43.  
  44. cout<<"source text :"<<test4<<endl;
  45. cout<<"number of words in the text = "<< GetNumberOfWords(test4)<<endl<<endl;
  46.  
  47. cout<< "THE CORRECT RESULT SHOULD BE: 4 WORDS\n";
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
WELCOME TO TEST OF FUNCTION size_t GetNumberOfWords(const char* text); 
source text :  Hello                  word            hello             word
number of words in the text = 4

source text :Hello                  word            hello             word
number of words in the text = 4

source text :Hello word hello word
number of words in the text = 4

source text :Hello word hello word  
number of words in the text = 4

THE CORRECT RESULT SHOULD BE: 4 WORDS