problem with strtok()

Thread Solved

Join Date: Aug 2008
Posts: 11
Reputation: smnadig is an unknown quantity at this point 
Solved Threads: 0
smnadig smnadig is offline Offline
Newbie Poster

problem with strtok()

 
0
  #1
Aug 19th, 2008
Hi All,

I am facing a problem with using the function strtok(). Following is just a sample code.
The code below stores the string str delimited by comma into an array.

I want to modify the code below so that array would contain an empty space/new line if a value is missing.

i.e., if str[] = source, desti, string1,,string3 ; then my array should hold an empty space/ new line in place of string2.

Can somebody help me with this?
Thanks in Advance

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. main()
  6. {
  7. char str[] = "source,desti,string1,string2,string3";
  8. char delims[] = ",";
  9. char *result = NULL;
  10. char myArray[10][50],*p;
  11.  
  12.  
  13. result = strtok( str, delims );
  14.  
  15. while( result != NULL )
  16. {
  17. printf( "%s \n", result );
  18. result = strtok( NULL, delims );
  19. }
  20.  
  21. getch();
  22.  
  23. }
Last edited by Ancient Dragon; Aug 19th, 2008 at 2:34 pm. Reason: add code tags
Thanks & Regards
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: problem with strtok()

 
0
  #2
Aug 19th, 2008
Maybe you could find the length of the string inbetween the commas. If it is zero then fill it with a space or newline.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: problem with strtok()

 
0
  #3
Aug 19th, 2008
strtok ignores empty fields. Edward would recommend using another solution, like strcspn and some method of reading or copying a substring:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. const char *str = "source,desti,string1,,string3";
  7. const char *delims = ",";
  8.  
  9. size_t start = 0;
  10.  
  11. while (str[start] != '\0') {
  12. size_t end = strcspn(str + start, delims);
  13.  
  14. printf("%.*s\n", end, str + start);
  15.  
  16. start += (str[start + end] != '\0') ? end + 1 : end;
  17. }
  18. }
The benefit of this approach is that you don't modify the original string anymore, so it works with string literals.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 11
Reputation: smnadig is an unknown quantity at this point 
Solved Threads: 0
smnadig smnadig is offline Offline
Newbie Poster

Re: problem with strtok()

 
0
  #4
Aug 20th, 2008
Originally Posted by Radical Edward View Post
strtok ignores empty fields. Edward would recommend using another solution, like strcspn and some method of reading or copying a substring:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. const char *str = "source,desti,string1,,string3";
  7. const char *delims = ",";
  8.  
  9. size_t start = 0;
  10.  
  11. while (str[start] != '\0') {
  12. size_t end = strcspn(str + start, delims);
  13.  
  14. printf("%.*s\n", end, str + start);
  15.  
  16. start += (str[start + end] != '\0') ? end + 1 : end;
  17. }
  18. }
The benefit of this approach is that you don't modify the original string anymore, so it works with string literals.
Thanks a lot It was very helpful
Thanks & Regards
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: ABuNeNe is an unknown quantity at this point 
Solved Threads: 0
ABuNeNe ABuNeNe is offline Offline
Newbie Poster
 
0
  #5
2 Days Ago
Hi guys, I'm new to C here. How do I assign the value to a variable printed from the line:

  1. printf("%.*s\n", end, str + start);

Please advise, thanks!
Reply With Quote Quick reply to this message  
Reply

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