Write a program to count number of words in a line

A line is taken from user and number of words is calculated..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. void main()
  5. {
  6. clrscr();
  7. char str[100];
  8. int w=1;
  9. cout<<"Please enter a String: ";
  10. gets(str);
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. for (int i=0;str[i]!='\0';i++)
  14. {
  15. if(str[i]==' ')
  16. w++;
  17. }
  18. cout<<"There are "<<w<<" words.";
  19. getch();
  20. }

Check out this stream