Console Word Counter in C++
Published: 2019-10-14 02:12:30

This is a simple programming exercise that will keep track of how many words there are in a given string and displays both the word count and average letters per word. To do this, we will create a counter that will count each letter. When we find a white space character we will then count that as the end of a word. Once we are finished iterating through the string, we will add all the letters of each word together and divide it by the number of words counted to get the average number of letters per word. The expected outcome should look something like this in the console: 
```
Enter a string of 80 characters or less: 
hello world      
The number of words in your string is: 2
The average number of letters per word is: 5
```
First, let's prompt the user for a string, or character array. For this program we're going to limit the input size to 80 characters. To get the user input, use `cin.getline` instead of `cin` because `cin` will only read a single word, skipping over any white space characters which is what we don't want. The program looks like this so far:
```
const int SIZE = 81;

int main () {
  char input[SIZE];
  cout << "Enter a string of 80 characters or less: " << endl;
}
```
Once we have a string, we will count the number of words by looping over it and incrementing a variable counter when the current character is equal to `' '`. Let's make a function called wordCount and pass in the input. The wordCount function should then expect a pointer or character array. In other words, `wordCount(char str[])` or `wordCount(char *str)` will work since arrays are automatically passed by reference. The wordCount function will return the num of words plus 1 to account for the last word since there is no white space character at the end of the string. wordCount function should look something like this:
```
int wordCount(char *str) {
  int numWords = 0;
  while(*str) {
    if(*str == ' ') {
      numWords++;
    }
    str++;
  }
  return numWords + 1;
}
```
Although counting the words and characters could be done together, for this purpose we'll create another function to count the number of characters in the string called letterCount. The letterCount function will count all characters excluding white space character. It should look similar to the wordCount function.
```
int letterCount(char *str) {
  int numLetters = 0;
  while(*str) {
    if(*str != ' ') {
      numLetters++;
    }
    str++;
  }
  return numLetters;
}
```
Now that we have both word count and total number of characters, we can calculate the average for the user. In the main function, set a variable to be equal the return value of `letterCount(input)` divided by the value of `wordCount(input)`. Don't forget that the variables holding the function results and average should be a double type variable to include decimals. 
```
double avg;
double numLetters = 0;
double numWords = 0;

numWords = wordCount(input);
numLetters = letterCount(input);
avg = numLetters / wordCount;
```
To finish the program, console log the number of words and average letters per word. And that's all for the word counter exercise. However, note that this is a simple solution and does not account for some catches like if the user only enters a white space character. The whole code looks like this: 
```
#include <iostream>
using namespace std;

int wordCount(char *str);
int letterCount(char *str);

int main()
{
  char input[SIZE];
  int numWords;
  double numLetters = 0;
  double avg;

  cout << "Enter a string of 80 characters or less: " << endl;
  cin.getline(input, SIZE);


  numWords = wordCount(input);
  cout << "The number of words in your string is: " << numWords << endl;
  numLetters = letterCount(input);
  avg = numLetters / numWords;
  cout << "The average number of letters per word is: " << avg << endl;

  return 0;
}

int wordCount(char *str) {
  int numWords = 0;

  while(*str) {
    if (*str == ' ') {
        numWords++;
    }
    str++;
  }
return numWords + 1;
}

int letterCount(char *str) {
    int numLetters = 0;

    while (*str) {
        if (*str != ' ') {
            numLetters++;
        }
        str++;
    }
    return numLetters;
}
```