-
Word Counter
Hi all,
I need some help figuring out a way to count words in C++. More specifically I need help in three issues. Let me back up a bit, the program that I have to do takes the keystrokes that I enter and it counts the number of lines, words and characters. Now counting words, believe it or not was the easy part for me. I just can't count the number of lines or the number of characters and its frustrating because all its giving me is garbage. Here's the code that I have so far:
Code:
#include <iostream.h>
#include <string.h>
int charactercount, linecount, wordcount;
void printCharacters( const char * );
int main() {
char blurb[150];
char *tokenPtr; //pointer used to count words
char *charPtr; //pointer used to count characters
//create a dynamic array that counts each character put into it
cin.getline( blurb, 100, '\n');
//The following four lines will count the number of words used.
tokenPtr = strtok( blurb, " " );
for (wordcount = 0; tokenPtr != NULL; wordcount++ ){
tokenPtr = strtok( NULL, " " );
}
printCharacters( blurb );
//The following lines will count the number of characters used.
for ( charcount = 0; *charPtr != '\0'; charPtr++){
cout << "charPtr: " << *charPtr;
}
cout << "line : " << linecount << ", word : " << wordcount
<< ", char : " << charactercount << "\n";
}
[code tags added by ygfperson]
Oh, and one more thing the array that I am supposed to be reading from does not have any previously allocated space since once the user executes the program all input is being taken into the keyboard until you hit the <Enter> button. How would I go about getting enough space in the array for any length of string(s) that the user types in before I manipulate his string? Thank you in advance for all those people that are helping me out.
-
>>tokenPtr = strtok(blurb, " ");
You do realise that using strtok() helps to destroy the string? In your code, you are changing all the spaces to nulls, so how do you expect to be able to reuse the string?
Number of characters: strlen() maybe.
Number of words: count the spaces, maybe using strchr() in a similar fashion to how you used strtok().
Number of lines: How many times are you calling getline()?
-
One more question...
Hey again all,
I have one more question with my word counter algorithm. Like I said earlier, when I execute the program, I should be able to write on the screen exactly what my string of words are going to be. The user is also allowed to span multiple lines when they push the <Enter> key. Is there a way to NOT let the computer taken control again until after the user hits a certain escape sequence? I have the getline() function in there but that would only do it for one line... the user can span as many lines as he wants to... here's my code so far:
(P.S. - Thank you for your help Hammer, your awesome)
Code:
#include <iostream.h>
#include <string.h>
int charcount, linecount, wordcount;
int main() {
char blurb[150];
char *tokenPtr; //pointer used to count words
char *charPtr; //pointer used to count characters
cin.getline( blurb, 150, '\n');
//The following lines will count the number of characters used.
tokenPtr = blurb;
//The following lines will count the number of words used.
tokenPtr = strtok(blurb, " ");
for (wordcount = 0;tokenPtr != NULL; wordcount++)
{
tokenPtr = strtok(NULL, " ");
}
cout << "line : " << linecount << ", word : " << wordcount
<< ", char : " << charcount << "\n";
-
Re: One more question...
>>Is there a way to NOT let the computer taken control again until after the user hits a certain escape sequence?
Yes, it's called EOF, which one of CTRL+D or CTRL+Z in most cases.
Code:
while ( cin.getline(string, maxLength) && !cin.eof() )
{
/* Do something */
}
In this example, we still process line by line though. There are probably other ways too...
-
This is what i would do for the thing:
Code:
char input;
int lines;
int words;
int characters;
while((input = getche()) != /*signal for quit*/)
{
++characters;
if(input == ' ')
++words;
else if(input == '\n')
++lines;
}
I haven't tried it, but it might work...