| | |
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:
Solved Threads: 8
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
thanks
c Syntax (Toggle Plain Text)
#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; }
C Syntax (Toggle Plain Text)
./a.out beforeToknization: 0 firstline firstToken:0 restOfLine:0
•
•
Join Date: Sep 2008
Posts: 1,594
Reputation:
Solved Threads: 200
I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html
Out.
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
•
•
•
•
I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html
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.
Last edited by csurfer; Jul 7th, 2009 at 12:19 am.
I Surf in "C"....
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
•
•
•
•
You can use strchr function with space as the delimiter to mark the boundary of the first token and then read it accordingly.
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:
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:
c Syntax (Toggle Plain Text)
#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; }
c Syntax (Toggle Plain Text)
#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; }
-Tommy (For Great Justice!) Gunn
"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
.
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.
•
•
Join Date: Sep 2008
Posts: 1,594
Reputation:
Solved Threads: 200
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 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.
![]() |
Similar Threads
- To form a palindrome of a given string (C)
- extract token(word) from string (C++)
- Problem with strtok() for data structure creation (C++)
- Help please in STRING (C++)
- String class (C++)
- Capitalize first char of every word in input string (C)
- Reading a string (C)
- help reading from .txt file (C++)
- strtok() function questions <C programming> (C)
Other Threads in the C Forum
- Previous Thread: function prototype
- Next Thread: I made the simplest program I could in Visual Studio and it won't work
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions test testautomation unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h





