Count words in a string

Count Words : -
  1. #include "stdafx.h"  // Header file for MS VS
  2. #include <iostream>  // Some compiler used < iostream.h>
  3. #include <conio.h>  // header file
  4. using namespace std ;
  5. int main ()
  6. {
  7. char input[100] ;
  8. int words  ;
  9.  cout << "Enter a string : " ;
  10. cin.getline(input , 100 , '\n') ;
  11.   if (input[0] == ' ')
  12. words = 0 ;
  13. else 
  14. words = 1 ;

  15. for(int i = 0 ; i < 100 ; i ++)
  16. {
  17. if (input[i] == ' ' && input[i + 1] != ' ')
  18.          words ++ ;
  19. }
  20. cout << "Words in your enterd string : "<< words ;
  21. _getch() ; // some compiler use getch()
  22. return 0 ;
  23. }
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment