954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

get the rest of the string after a strtok

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

#include <stdio.h>
#include <string.h>

int main(){
  const char* line= "0 firstline";
  char* l = strdup(line);
  printf("beforeToknization:\t%s\n",l);
  char *tok = strtok(l," ");
  printf("firstToken:%s\trestOfLine:%s\n",tok,l);
  return 0;
}
./a.out
beforeToknization:      0 firstline
firstToken:0    restOfLine:0
monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

You can use strchr function with space as the delimiter to mark the boundary of the first token and then read it accordingly.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 
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

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

All strtok does is replace the delimiter with '\0'. If you skip over it, you'll find the rest of the string:

#include <stdio.h>
#include <string.h>

int main()
{
    char s[] = "a|b|c|d|e|f|g";
    char *tok = strtok(s, "|");

    printf("%s\n%s\n", tok, tok + strlen(tok) + 1);

    return 0;
}

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:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char s[1024];
    char *p;

    printf("Type a sentence: ");
    fflush(stdout);

    if (!fgets(s, 1024, stdin)) return EXIT_FAILURE;
    if (p = strchr(s, '\n')) *p = '\0';

    if (!strpbrk(s, " \t\r\v")) printf("%s\n", s);
    else
    {
        char *tok = strtok(s, " \t\r\v");

        printf("%s\n%s\n", tok, tok + strlen(tok) + 1);
    }

    return 0;
}
Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

My mistake, monkey king.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Why would you give me bad reputation for admitting I made a mistake? That doesn't make very much sense.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
I wanted to know why you would disapprove of someone admitting a mistake, or why you would even get involved.

(1) i'm not "disapproving of you" or your "admission of a mistake" -- i negatively marked a post that was both inappropriate and unhelpful. sorry if it hurt your feelings.

(2) as to getting involved, let me ask this: For someone who admits he "doesn't know C programming" why areYOU even trying to answer questions in a C language forum? dont you have some PHP to go look at or something?

(3) we're talking about what was a net loss of six (6) freaking points. are your little feelings that sensitive? are you that wrapped up in reputation points as some measure of your personal self worth?

I mean, get a life already. quit being such a whiner. if you recall, old J.C. wasn't a whiner. He manned up and took all the neg rep handed to him.

i dont think you're doing a good job at all of measuring up to your self-appointed namesake. maybe you should change it to "BestJewSinceSimonPeter", because as i remember, that guy was always complaining about something or other.


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

I thought you marked my second post for bad rep, not the first, hence the confusion. But like I said, why is this such a big deal to you? Hmm. And I do "know C programming", I admit to not knowing it very well. That doesn't mean I can't answer common issues. Also, I usually browse this forum and read threads I'm interested in. Who are you to tell me what forums I can't post in? While I can see you having a problem with my original post (which is why I apologized to begin with), your immaturity is incredible.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
But like I said, why is this such a big deal to you?

ahaha. nowthat's funny

your immaturity is incredible.

yeah, yeah, yeah... :icon_rolleyes: you've been talkin' to my wife, haven't you?

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

BestJew, did you just give me negative rep? Oh, look at you, you're seriously pissed off about this, arent' you? How adorable!

LOL!

oh, my sides...

:)

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You