I have to create a word counter function which counts the words of a string. When I run the program, it doesn't end. This is what I have created so far:
//-------------Start of Program------------->
#include <iostream>
#include <cstdlib>
using namespace std;
//Function Prototype
void stringLength(char *);
void reverseString(char *);
void wordCount(char *);
//----------------Start Main Function------------------->
int main()
{
const int INPUT_SIZE = 80;
char input[INPUT_SIZE];
//Get the user's desired string
cout << "Please enter a phrase or sentence: \n\n";
cin.getline(input, INPUT_SIZE); //Read input as a string
//Display number of characters
cout << "\nThe entered string is ";
stringLength(input);
cout << " characters. \n";
//Display string backwards
cout << "The entered string backwards is: ";
reverseString(input);
cout << endl;
//Display number of words in the string
cout << "The number of words in the entered string is: ";
wordCount(input);
cout << endl;
}
//<------------End Main Function----------------------->
//<------------Start String Length Function------------>
void stringLength(char *string1)
{
int stringCount = 0;
stringCount = strlen(string1);
cout << stringCount;
}
//<------------End String Length Function-------------->
//<------------Start Reverse String Function----------->
void reverseString(char *string2)
{
char *revString = string2;
while(*revString != '\0')
++revString;
while (revString != string2)
cout.put(*--revString);
}
//<-----------End Reverse String Function-------------->
//<------------Start Reverse String Function----------->
void wordCount(char *string3)
{
int numWords = 0;
while(string3 != '\0')
numWords++;
cout << numWords;
}
//<-----------End Reverse String Function-------------->