Operating System - HP-UX
1834645 Members
1998 Online
110069 Solutions
New Discussion

Re: strtok question again

 
etienne_7
Occasional Advisor

strtok question again

Hello,
I am trying to parse an ASCII file using strtok. The trouble is some string that I am parsing contain one space and I am trying to get them all at once:
From the string:
Hello how are you
I want the following:
Hello
how
are you
In 3 different strings........
So far I am using the following code:
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, " \t");
i++;
} while( (pValue != NULL) && (i < Column) );

what I get is:
Hello
how
are
you.....
In fact, I would like strtok to differ from one space to two spaces....
getting only 3 strings not 4:

For the string:
"1234 1235 152153 1 111"

I would like to get only 4 strings:
"1234"
"1235"
"152153"
"1 111"

and not
"1"
12 REPLIES 12
Biswajit Tripathy
Honored Contributor

Re: strtok question again

Did you see my reply to your other strtok question?
Did that help?

Looks like you need to modify the loop to the
following:

do
{
if (i < Column-1)
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );
:-)
etienne_7
Occasional Advisor

Re: strtok question again

Ok, I will try and did saw your reply but couldn't answer using the thread....will try this time.
The issue is that the single space could be anytim in the string, not only at the end.
etienne_7
Occasional Advisor

Re: strtok question again

Run time error while doing that....

// scan to correct column
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );

This doesn't seem to work...any other idea?
Biswajit Tripathy
Honored Contributor

Re: strtok question again

I don't see the if() condition inside the loop in your
code as I suggested.

What is the value of Column?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

Column is 5, sometimes 6....Doesn't rreally matter, strtok crashes when you put NULL, NULL
Biswajit Tripathy
Honored Contributor

Re: strtok question again

It does not crash on my 11.11 system. Is your string
NULL terminated?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

It crashes before the end of the string, at the first pass.
Biswajit Tripathy
Honored Contributor

Re: strtok question again

Can you post your full program?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

include is the function I am using.
tx
Biswajit Tripathy
Honored Contributor

Re: strtok question again

Here is my code:

#include
#include

int main ()
{
int i, Column;
char pText[512];
char *pValue;

/* Your first string */
i = 0;
strcpy (pText, "Hello how are you");
printf ("----> Your first string = \"%s\"\n", pText);
Column = 2; /* Set Column to # of words in string minus 2 */
pValue = strtok (pText, " \t");
printf ("pValue=\"%s\"\n", pValue);
do {
if (i < Column - 1 )
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, "\0");
i++;
printf ("pValue=\"%s\"\n", pValue);
} while( (pValue != NULL) && (i < Column) );

/* Your second string */

i = 0;
strcpy (pText, "1234 1235 152153 1 1111");
printf ("----> Your Second string = \"%s\"\n", pText);
Column = 3; /* Set Column to # of words in string minus 2 */
pValue = strtok (pText, " \t");
printf ("pValue=\"%s\"\n", pValue);
do {
if (i < Column - 1)
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, "\0");
printf ("pValue=\"%s\"\n", pValue);
i++;
} while( (pValue != NULL) && (i < Column) );



return 0;
}
:-)
etienne_7
Occasional Advisor

Re: strtok question again

Your code is actually working but I think I was not explicite enough on what I needed, I apologise for that.
Here is a screen capture of your code working....(see attached)

I want the strtok to differ when there is one space and 2 spaces, when there are 2 space, it is a different word, when it is 1 space it belongs to the same name...

"Hello how are you? good me too."
This will become:
Hello
how
are you?
good me
too.
As you can see, when there was more than one space, it considered it as 2 different string but when there was only one space the it put it together.
Thanks for your help already, I liked it the way you did the first program, looking for the end of string "\0".
rgds
Biswajit Tripathy
Honored Contributor

Re: strtok question again

etienne wrote:
> I want the strtok to differ when there is one
> space and 2 spaces, when there are 2 space, it is
> a different word, when it is 1 space it belongs to
> the same name...

To do this, I would probably not use strtok. I would
use strchr() to search for single space char in a for
loop and when found, I would print and decide if I
need to print a space or a newline by looking at the
next char. Only pass the unprocessed part of the
input string as the first parameter to strchr().

I'm not loged into a HP-UX machine rightnow, so
can't give you an example code; but it should be
quite simple.

- Biswajit
:-)