| | |
problem with strtok()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> #include <string.h> main() { char str[] = "source,desti,string1,string2,string3"; char delims[] = ","; char *result = NULL; char myArray[10][50],*p; result = strtok( str, delims ); while( result != NULL ) { printf( "%s \n", result ); result = strtok( NULL, delims ); } getch(); }
Last edited by Ancient Dragon; Aug 19th, 2008 at 2:34 pm. Reason: add code tags
Thanks & Regards
strtok ignores empty fields. Edward would recommend using another solution, like strcspn and some method of reading or copying a substring:
The benefit of this approach is that you don't modify the original string anymore, so it works with string literals.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main() { const char *str = "source,desti,string1,,string3"; const char *delims = ","; size_t start = 0; while (str[start] != '\0') { size_t end = strcspn(str + start, delims); printf("%.*s\n", end, str + start); start += (str[start + end] != '\0') ? end + 1 : end; } }
If at first you don't succeed, keep on sucking until you do succeed.
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
strtok ignores empty fields. Edward would recommend using another solution, like strcspn and some method of reading or copying a substring:
The benefit of this approach is that you don't modify the original string anymore, so it works with string literals.c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main() { const char *str = "source,desti,string1,,string3"; const char *delims = ","; size_t start = 0; while (str[start] != '\0') { size_t end = strcspn(str + start, delims); printf("%.*s\n", end, str + start); start += (str[start + end] != '\0') ? end + 1 : end; } }
It was very helpful Thanks & Regards
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#5 22 Days Ago
Hi guys, I'm new to C here. How do I assign the value to a variable printed from the line:
Please advise, thanks!
C Syntax (Toggle Plain Text)
printf("%.*s\n", end, str + start);
Please advise, thanks!
![]() |
Similar Threads
- Problem with strtok() for data structure creation (C++)
- Search string in a text file (C)
- using strtok() to populate and array (C)
- struct linked list problem (C)
- int problem (C++)
- Problem with a snippet of code in a shell program (C)
- Strtok() (C)
Other Threads in the C Forum
- Previous Thread: Dynamic include in C
- Next Thread: How to output all the letters of the alphabet randomly?
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






