# beginner question about strings

• 09-05-2008
gp364481
beginner question about strings
Hey, new here
I have a problem.

I need to read in a string of chars (upper/lower case letters and punctuation marks : '.' or ',' or ';' or '?' or '!') of unspecified length, then take each one of the words and process some calculations such as : average length of a word, average vowels in each word, how many words are between 10 and 15 letters long etc..

An example input would be:
Hello there! How are you?

I read a string in using the keyboard and I use strtok from the <string.h> library to break the string down to individual words. The problem is that the words in the string can be seperated by any white space characters not just by the spacebar.
Iam using something like :
char line[MAX_SIZE];
strtok( line, " ");
so I can break the string into words. But I am only limited to words that are seperated by the spacebar " ".
How do I take each word seperately from the input string and run the calculations on it ?

gp
• 09-05-2008
kpreston
Quote:

Originally Posted by gp364481
Hey, new here
I have a problem.

I need to read in a string of chars (upper/lower case letters and punctuation marks : '.' or ',' or ';' or '?' or '!') of unspecified length, then take each one of the words and process some calculations such as : average length of a word, average vowels in each word, how many words are between 10 and 15 letters long etc..

An example input would be:
Hello there! How are you?

I read a string in using the keyboard and I use strtok from the <string.h> library to break the string down to individual words. The problem is that the words in the string can be seperated by any white space characters not just by the spacebar.
Iam using something like :
char line[MAX_SIZE];
strtok( line, " ");
so I can break the string into words. But I am only limited to words that are seperated by the spacebar " ".
How do I take each word seperately from the input string and run the calculations on it ?

gp

Are you saying one of your problems are words like these: there!, you?
• 09-05-2008
gp364481
My problem is input like this:

Hello [tab] there how are [tab] you doing?

because strtok( string, " "); takes a space as an arguement, my program will break down the string everytime it finds a single space as follows:

Hello [tab] there
how
are [tab] you
doing?

I want it to be:
Hello
there
how
are
you
doing?

thanks

Edit: i dont know how to display tabbed text in the forums so where u see [tab] it means the user hit the tab button
• 09-05-2008
tabstop
You can put multiple things in your tokenizer string. You might want something like " \t\n".
• 09-05-2008
gp364481
Quote:

Originally Posted by tabstop
You can put multiple things in your tokenizer string. You might want something like " \t\n".

thanks! u saved the day

...for now ;0