Monday, 30 April 2012

Counting the number of Words in C++


#include <iostream>

using namespace std;

int main()
{
  char array[81];
  char* cptr;
  int words = 0;

  cout << "Enter a array of words:" << endl << endl;
  cin.getarray(array, 81);
  
  for (cptr = array; *cptr == ' '; ++cptr)
    ;
  while (*cptr != '\0')
  {
    ++words;   

    for ( ; (*cptr != ' ') && (*cptr != '\0'); ++cptr)
      ;
    for ( ; *cptr == ' '; ++cptr)
      ;
  }
  cout << "\nThe array you entered has " << words << " words."
       << endl;

  return 0;
}

No comments:

Post a Comment