/*******************************************************************************
* Aiden Stannard // CS1A // Chapter 10 Homework: Challange 3# // 11/28/2026
* _____________________________________________________________________________
*
* Word Counter
* _____________________________________________________________________________
*
* This program recieves a string of words and outpts the number of words in
* that string
* _____________________________________________________________________________
*
* Input:Enter a string:___
*
* Output: Word count:___
******************************************************************************/
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int countWords(const char *); // word counter function prototype
int main() {
char input[256]; //
cout << "Enter a string: ";
cin.getline(input, 256);
/* insures that the entire input is assigned to "input" when the user enters
a space bar between different words*/
cout << "Word count: " << countWords(input) << endl;
// Passes user input to function
return 0;
}
int countWords(const char* str) {
int count = 0;
bool inWord = false; // initialized bool value to a safe false value
while (*str != '\0') { // loop stop iterating if str becomes equal to a null
if (isspace(*str)) // determines if
{
inWord = false;
}
else if (!inWord)
{
inWord = true;
count++;
}
str++;
}
return count;
}