Word Counter
Jun 10, 2010 at 1:41am UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
//-------------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-------------->
Jun 10, 2010 at 2:24am UTC
I figured it out LOL. I will post it here for others.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//-------------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-------------->
Jun 10, 2010 at 4:31am UTC
usafsatwide, can you tell us what you figured out?
Jun 10, 2010 at 8:43am UTC
You do char *revString = string2; and its maybe fault becaus you dont reserve memory for revString, you dont know in
where direction of memory is.
You can try to revString=malloc(tam*sizeof(char));
And other things is
while(*revString != '\0')
++revString;
if you start in end of string the first dostn have '\0' this symbol is in the end
Topic archived. No new replies allowed.