Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

more string checking

Status
Not open for further replies.

tomchesley

Technical User
Apr 16, 2006
17
US
I need a way to determine a text string if it is ok or not
examples
id's can be any hex number up to ffffffff
a = bad missing ^id
^0 = bad missing name before ^id
Tron^0 =0k valid name and id
John^48d7cf5 =ok valid name an id
Tonka^0^MAKIABDCCBOC/ZONA.SIT/circuit/+/4/1,8 = ok hosting game
Everett^48d7cf5^MJBFOLJFBILF//rally/+/1/1,8 =ok hosting game
Tonka^0^MJBFOLJFBILF =ok joined game

the pointer that contains this string is source_p->info
i can check for the len of this with

nlen = strlen(source_p->info);

but not sure how i can check for the ^ then the contents after or before

any hints?
 
Try
Code:
char *before, *after;

before = strtok (source_p->info, "^");
after  = strtok (NULL, "^");
before is the string before ^
after is the string after ^

 
that works but it changes the original value
I tried
Code:
char *before, *after, *hold;
hold = source_p->info;
before = strtok (hold, "^");
after  = strtok (NULL, "^");
and it still changes the contents of source_p->info
 
You didn't say that you didn't want the original value changed.
Code:
char* hold = malloc (strlen (source_p->info) + 1);
strcpy (hold, source_p->info);
before = strtok (hold, "^");
after  = strtok (NULL, "^");
 
xwb is sharp and I was going to let him ride, but...;)
Code:
BUGS
       Avoid using these functions.  If you do use them, note that:

              These functions modify their first argument.

              These functions cannot be used on constant strings.

              The identity of the delimiting character is lost.

              The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
There are better ways to deal with this but they'll not come cleanly from string.h and libc imho.



 
Well I thought it did the trick.
but if the string doesnt have a ^ it crashs
 
Then you're going to have to check to see if before is NULL before proceeding.

Lee
 
DOH, didnt think of that thanks, but i checked both before and after for NULL
Now im set
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top