better way than strtok?


 
Thread Tools Search this Thread
Top Forums Programming better way than strtok?
# 1  
Old 10-05-2005
better way than strtok?

Hi all,

Right now I'm using this but it seems to be a hack:

if (prefix(arg, "mark=")) {
for (markid = strtok(args[0],"="); markid; markid=strtok((char *)NULL, "=")) {


basically the user passes "mark=ny" to the command. I want to be able to extract "ny" from that so I call strtok() twice.

I'm assuming there must be a better way....anyone?

Thanks
# 2  
Old 10-05-2005
strchr will return the position of the first '=' in the string without having to modify the string itself.
Code:
markid=strchr(args[0],'=')+1;

It will return the "=ny" part, so incrementing it one from there will make it return the "ny" part. This assumes that the string includes '=' in it, if it doesn't it will break. But that's taken care of by prefix.
# 3  
Old 10-05-2005
Quote:
Originally Posted by Corona688
strchr will return the position of the first '=' in the string without having to modify the string itself.
Code:
markid=strchr(args[0],'=')+1;

It will return the "=ny" part, so incrementing it one from there will make it return the "ny" part. This assumes that the string includes '=' in it, if it doesn't it will break. But that's taken care of by prefix.
thanks!

but the catch here (which I just realized) is I need to know if they ever pass just "markid=". This means they want to clear the field....

So with the above suggestion, when they passed in "markid=", I would get NULL I assume?
# 4  
Old 10-05-2005
Since the input still includes a '=', strchr would still find one. Moving one beyond that would get you to the NULL terminator, aka you'd have an empty string. You can easily test that by looking for the null terminator:
Code:
if(markid[0] =='\0') { /* stuff */ }

# 5  
Old 10-05-2005
Quote:
Originally Posted by Corona688
Since the input still includes '=', strchr would still fine one. Moving one beyond that would get you to the NULL terminator, aka you'd have an empty string. You can easily test that by looking for the null terminator:
Code:
if(markid[0] =='\0') { /* stuff */ }

good point. thanks so much!! this is perfect.
# 6  
Old 10-05-2005
No prob. This is the fastest I've ever seen a thread move here, it's lucky we were both online at the same time Smilie
# 7  
Old 10-05-2005
Quote:
Originally Posted by Corona688
No prob. This is the fastest I've ever seen a thread move here, it's lucky we were both online at the same time Smilie
Smilie yeah any other UNIX/C boards that are a little more active?
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

strtok() gives segmentation fault!!

#include<iostream.h> #include<string> #include<stdio.h> int main() { char *cmd="delete backup backup-iso image a.iso b.iso c.iso d.iso"; char *tokenized_cmd,*sub_cmd; sub_cmd=strstr(cmd,"image"); tokenized_cmd=strtok(sub_cmd," "); ... (3 Replies)
Discussion started by: ashwini.engr07
3 Replies

2. Programming

strtok with while loops

Why is line (null) after the first while loop run? (keyword does jump to the next word.) #include <ftw.h> #include <stdio.h> #include <string.h> char filenames = ""; int list(const char *name, const struct stat *status, int type) { if( (type == FTW_F) && strstr(name, ".txt") &&... (3 Replies)
Discussion started by: cyler
3 Replies

3. Programming

How to use strtok twice in the same program?

string str1(" 1 2 3 4 512543 "); string str2; if(str2.empty()) str2=str1; cout << "str2:" <<str2 <<endl; p1=strtok((char *)str1.c_str()," "); while(p1) { ... (3 Replies)
Discussion started by: sathishkmrv
3 Replies

4. Shell Programming and Scripting

strtok equivalent in perl

Hi All, Is their any equivalent for strtok (in c) to use in perl script. Thanks in advance. JS (1 Reply)
Discussion started by: jisha
1 Replies

5. Programming

Regardign strtok() output directing to 2-D string array

Hi, I just wrote a program in C to split a comma seperated string in to group of strings using strtok() function. The code is: int main() { char *temp;//not used here but basically we extract one string after another using strtok() and assign to a string pointer defined like this. ... (3 Replies)
Discussion started by: SankarV
3 Replies

6. Linux

Strtok function....

can any help me out y dis program is giving me a segmentation fault..... #include<stdio.h> #include<string.h> int main() { char *str="Tanvir/home/root/hello"; const char *d ="/"; char *ret; ret=strtok(str,d); if(ret==NULL) printf("NULL NULL"); else ... (3 Replies)
Discussion started by: Tanvirk
3 Replies
Login or Register to Ask a Question