943,862 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4001
  • C RSS
Aug 19th, 2008
0

problem with strtok()

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smnadig is offline Offline
11 posts
since Aug 2008
Aug 19th, 2008
0

Re: problem with strtok()

Maybe you could find the length of the string inbetween the commas. If it is zero then fill it with a space or newline.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 19th, 2008
0

Re: problem with strtok()

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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 20th, 2008
0

Re: problem with strtok()

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smnadig is offline Offline
11 posts
since Aug 2008
Nov 24th, 2009
0
Re: problem with strtok()
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ABuNeNe is offline Offline
1 posts
since Nov 2009

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.
Message:
Previous Thread in C Forum Timeline: Dynamic include in C
Next Thread in C Forum Timeline: How to output all the letters of the alphabet randomly?





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


Follow us on Twitter


© 2011 DaniWeb® LLC