Friday, 7 December 2012

CPP Program to find words in a string


#include<iostream.h>
#include<conio.h>
class word
{ private:
          char s1[80];
  public:
          void wordcount();
          int wordend(char);
};
void word:: wordcount()
{        cout<<" Enter a line of text :";
          cin.getline(s1,80);
          int wordend(char);
          int i,count=1;
          for(i=0;s1[i]!='\0';i++)
          {        if((wordend(s1[i]))&&(!wordend(s1[i+1])))
                   count++;

          }
          cout<<" String contains "<<count<<" words.";
}

int wordend(char c)
{        switch (c)
          { case '.':
          case ';':
          case ',':
          case '\t':
          case ' ':return 1;
          default:return 0;
          }
}

void main()
{        clrscr();
          word w;
          w.wordcount();
          getch();
}

No comments:

Post a Comment