get the rest of the string after a strtok

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

get the rest of the string after a strtok

 
0
  #1
Jul 6th, 2009
If I simply just want to tokenize the first element of a string and then output the remainder of the string, how can this be accomplished?

thanks
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(){
  5. const char* line= "0 firstline";
  6. char* l = strdup(line);
  7. printf("beforeToknization:\t%s\n",l);
  8. char *tok = strtok(l," ");
  9. printf("firstToken:%s\trestOfLine:%s\n",tok,l);
  10. return 0;
  11. }


  1. ./a.out
  2. beforeToknization: 0 firstline
  3. firstToken:0 restOfLine:0
Quick reply to this message  
Join Date: Sep 2008
Posts: 1,594
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: get the rest of the string after a strtok

 
0
  #2
Jul 6th, 2009
I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html
Out.
Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: get the rest of the string after a strtok

 
0
  #3
Jul 7th, 2009
Originally Posted by BestJewSinceJC View Post
I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html
I'm aware of the fact that I can tokenize the entire string,
but if I (dependent on the first token) just want the rest of the line,
without the need to tokenize it. How do I do that
Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: get the rest of the string after a strtok

 
0
  #4
Jul 7th, 2009
You can use strchr function with space as the delimiter to mark the boundary of the first token and then read it accordingly.
Last edited by csurfer; Jul 7th, 2009 at 12:19 am.
I Surf in "C"....
Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: get the rest of the string after a strtok

 
0
  #5
Jul 7th, 2009
Originally Posted by csurfer View Post
You can use strchr function with space as the delimiter to mark the boundary of the first token and then read it accordingly.
Ahh, cheers
Thanks!
This will come in handy
Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: get the rest of the string after a strtok

 
1
  #6
Jul 7th, 2009
All strtok does is replace the delimiter with '\0'. If you skip over it, you'll find the rest of the string:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. char s[] = "a|b|c|d|e|f|g";
  7. char *tok = strtok(s, "|");
  8.  
  9. printf("%s\n%s\n", tok, tok + strlen(tok) + 1);
  10.  
  11. return 0;
  12. }
The only problem is there is no way to tell if strtok is finished until it returns NULL, and by then it's too late. If you know that there is more than one token, this works, but if not, you have to use something other than strtok to get the first token or do some extra testing:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. char s[1024];
  8. char *p;
  9.  
  10. printf("Type a sentence: ");
  11. fflush(stdout);
  12.  
  13. if (!fgets(s, 1024, stdin)) return EXIT_FAILURE;
  14. if (p = strchr(s, '\n')) *p = '\0';
  15.  
  16. if (!strpbrk(s, " \t\r\v")) printf("%s\n", s);
  17. else
  18. {
  19. char *tok = strtok(s, " \t\r\v");
  20.  
  21. printf("%s\n%s\n", tok, tok + strlen(tok) + 1);
  22. }
  23.  
  24. return 0;
  25. }
-Tommy (For Great Justice!) Gunn
Quick reply to this message  
Join Date: Sep 2008
Posts: 1,594
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: get the rest of the string after a strtok

 
0
  #7
Jul 7th, 2009
My mistake, monkey king.
Out.
Quick reply to this message  
Join Date: Sep 2008
Posts: 1,594
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: get the rest of the string after a strtok

 
0
  #8
Jul 8th, 2009
Why would you give me bad reputation for admitting I made a mistake? That doesn't make very much sense.
Out.
Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: get the rest of the string after a strtok

 
1
  #9
Jul 8th, 2009
Originally Posted by BestJewSinceJC View Post
Why would you give me bad reputation
"i dont know but here's google" may be appropriate in some cases, but Monkey_King posted a decent question, included formatted code, described the output he wanted, and asked why wasnt it working exactly the way he anticipated.

And the answer is not so easy, because strtok() is a difficult function for inexperienced programmers to understand from just a basic description.

to post a snarky, unhelpful RTFM-style answer is totally appropriate for newbs or other lazy sods who just want someone to do their work for them. But Monkey_King, as evidenced by the clarity of his post and the effort he made prior to posting, is obviously not in that group.

there are plenty of unhelpful or inappropriate posts on these forums. But i found your Post #2 to be both unhelpful and inappropriate so i neg-repped it. tux4life, as it turns out, disagreed with me so he pos-repped it.

now measured by net gain, you have come out ahead, gaining even more more rep points than Tom Gunn, the one person to actually post a thoughtful and correct solution.

so be proud. you scored. and for someone who "doesn't know, but can use google", you did a good job whining for your rep




.
Last edited by jephthah; Jul 8th, 2009 at 2:16 pm.
Quick reply to this message  
Join Date: Sep 2008
Posts: 1,594
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: get the rest of the string after a strtok

 
0
  #10
Jul 8th, 2009
Let's review:

I misread his question, then made a mistake. I then realized this mistake, admitted my mistake, and apologized to monkey king. I didn't whine for rep nor did I ask anyone for rep. I wanted to know why you would disapprove of someone admitting a mistake, or why you would even get involved.
Last edited by BestJewSinceJC; Jul 8th, 2009 at 2:46 pm.
Out.
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC