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.