943,724 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1719
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 6th, 2009
0

get the rest of the string after a strtok

Expand Post »
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
Similar Threads
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 6th, 2009
0

Re: get the rest of the string after a strtok

I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 7th, 2009
0

Re: get the rest of the string after a strtok

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
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 7th, 2009
0

Re: get the rest of the string after a strtok

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 7th, 2009
0

Re: get the rest of the string after a strtok

Click to Expand / Collapse  Quote originally posted by csurfer ...
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
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 7th, 2009
1

Re: get the rest of the string after a strtok

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. }
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 7th, 2009
0

Re: get the rest of the string after a strtok

My mistake, monkey king.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 8th, 2009
0

Re: get the rest of the string after a strtok

Why would you give me bad reputation for admitting I made a mistake? That doesn't make very much sense.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 8th, 2009
1

Re: get the rest of the string after a strtok

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 8th, 2009
0

Re: get the rest of the string after a strtok

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: function prototype
Next Thread in C Forum Timeline: I made the simplest program I could in Visual Studio and it won't work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC