Manipulating words in a string of characters

This is a discussion on Manipulating words in a string of characters within the C Programming forums, part of the General Programming Boards category; Hey, new to C here
I'm writing a program for school.
I need to read in input from the keyboard ...

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:

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?

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?

...

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.

You don't need to copy it.
Aside from that, you also need to get rid of gets. See http://cpwiki.sourceforge.net/Gets.
A pointer behaves the same as an array (plus when passing an array to a function, it becomes a pointer anyway), so you can just use the pointer returned by strtok directly.

You don't need to copy it.
Aside from that, you also need to get rid of gets. See http://cpwiki.sourceforge.net/Gets.
A pointer behaves the same as an array (plus when passing an array to a function, it becomes a pointer anyway), so you can just use the pointer returned by strtok directly.

You said it behaves the same as an array.
So if there exists a pointer called char *tokenPtr; that points to the value "hello",
is an expression such as tokenPtr[1]; valid? does it hold the value 'e' ?

Hehe. What you're doing there is *almost* correct. In fact, you just need to get rid of the & (ampersand). The ampersand indicates that you want the address of the variable it belongs to. So you are in effect returning the address of tokenPtr[j], printf then tries to convert that into a character. I'm guessing that the addresses translate into some non-printable characters, which is why you're seeing spaces.
Also, you don't really need that line. printf ("%s", tokenPtr); will work just fine as long as it is NULL terminated.

QuantumPete
Edit: Also fyi, sizeof (tokenPtr) will always return 4, because tokenPtr is a pointer and unless you're working on a 64bit system, your pointers are size 32bits = 4 bytes.

For the purposes in your code gets can be directly replaced with fgets(line, sizeof(line), stdin) and there will be no noticeable difference, except that if anyone ever enters a line longer than MAX_SIZE, the program will still work just as fine as before [ok, so if a word straddles the 1000 character point, it will be broken into two, but that is a much less serious problem than your program crashing under those same circumstances].