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

Recommended Answers

All 13 Replies

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

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

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

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;
}
commented: The most useful post in this thread :) +12

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

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


.

commented: After reading this post I actually do not disagree with you, in fact I agree with you, but the sentence: "come on man" wasn't an informing description of the bad rep. +12

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.

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 are YOU 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.


.

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.

But like I said, why is this such a big deal to you?

ahaha. now that's funny

your immaturity is incredible.

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

.

commented: dude's a bully. :) -1

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...

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.