//Cecilia Rangel CSC5 Chapter 10, P.588, #3
//
/**************************************************************
*
* COMPUTE WORD COUNTER
* ____________________________________________________________
* This program returns an integer and accepts a
* pointer to a C-string as an arguement and returns the
* number of words contained in the string.
*_____________________________________________________________
* INPUT
* userinput : INPUT a string from the user
*
* OUTPUT
* words : OUTPUT the amount of words in the string
*
* ***************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int CountNumWords(char* ptr);
int main()
{
//
//Declare variables and pointers
int SIZE = 100; //Initialize size to 100
char userInput[SIZE]; //User input
char* ptr; //Pointer to character type
ptr = userInput; //Assign pointer to array
int numWords; //Number of words
//
//Prompt user to enter character string
cout << "Enter a character string.\n";
cin.getline(userInput, SIZE);
//
//Call to function
numWords = CountNumWords(ptr);
//
//Output the number of words in string
cout << "The number of words in your string is ";
cout << numWords;
return 0;
}
//
//Return number of words
int CountNumWords(char* ptr)
{
int count;
int size = 0;
int numWords = 0;
//
//Find size of string
for (count = 0; *(ptr + count) != '\0'; count++)
size++;
//
//Find number of words
for (count = 0; count <= size; count++)
{
if (*(ptr + count) == ' ' || *(ptr + count) == '\0')
numWords++;
}
return numWords;
}