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-------------->

What is the output of this program? I mean does it display the number of words in the string (which is the last part of the program)?

I figured it out LOL. I will post it here for others.

//-------------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 = 100;
	char input[INPUT_SIZE];

	//Get the user's desired string
	cout << "Please enter a phrase or sentence (up to 100 characters):  \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 << "\n\n";
}
//<------------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 Word Count Function----------->
void wordCount(char *string3)
{
	int numWords = 1;
	while(*string3 != '\0')
	{
		if(*string3 == ' ')
			numWords++;
		string3++;
	}
	cout << numWords;

}

//<-----------End Word Count Function-------------->

Right now! I dont really know what is wrong but why dont you just try one thing, that is, declare an extra variable and at the end of your program write a cin statement. With all this stuff, you should get an output (of all three parts) and after that it will wait for an input.

I hope it helps ;)

Edited 6 Years Ago by achieve_goals: n/a

This article has been dead for over six months. Start a new discussion instead.