Manipulating words in a string of characters
Hey, new to C here
I'm writing a program for school.
I need to read in input from the keyboard (it could be 2 words or 1 page worth of input).
Then I need to take each one of the words and put in into a char array and run some calculations on them (how many vowels are there, how many total letters, etc.)
I have something like this:
Code:
char line[MAX_SIZE]; // array for whole input of the user
char *tokenPtr // a char pointer
char word[16]; //array for individual words
printf("Enter String: "); //prompt for string
gets(line);
tokenPtr = strtok( line, " \t\n"); //begin tokenizing sentence
while(tokenPtr !=NULL) {
tokenPtr = strtok(NULL, " \t\n"); //get next token
}//end while
So for an input of : "hello world" , tokenPtr would first hold "hello" and then after the while loop it would hold "world".
I would like to put the value that tokenPtr points to into a char array named word[] so i can calculate the number of vowels, the number of letters etc. Is there a way I can do that or has my approach been wrong so far?
Ultimately i would like to have something like:
Code:
while(tokenPtr != NULL) {
functionCalls();
.
.
.
tokenPtr = strtok(NULL, " \t\n");
} //end while
Is it possible to pass a char pointer such as tokenPtr into a char array and then run calculations on that array? If not, how can I approach this problem.
Sorry about the long post oO