Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtok with comma delimited string

Status
Not open for further replies.

jtrapat1

Programmer
Jan 14, 2001
137
0
0
US
I'm trying to parse a comma delimited string with strtok -
It's working fine but I have a couple of special cases:
I'm reading from a file and one line of my 320-character string looks like this:
JONES, SMITH, ABBEY, JOHNSON, LONG ISLAND, STATEN ISLAND, ...
My program can handle most of these values but it has a problem with items such as LONG ISLAND.
It treats these as two separate tokens: LONG and ISLAND.
Is there any way to modify my code so I can handle these values?
Here's my code:
-------------------------------------------------
/* declared in main() */
char seps[] = ", ";
char *token;
char req_tmp[320+1];
/* where I read the string from the file */
strcpy(req_tmp, adds.req_by);
token = strtok(req_tmp, seps);

while(token != NULL)
{
strcpy(bp70.last_name,token);
get_mbr_id();
token = strtok(NULL, seps);
}
----------------------------------------------------
I tried different delimiters but because of my file layout, a blank space and then a comma separating each item, it gets confused near LONG ISLAND and treats this as two separate items.
Is there a way to modify my code to correctly parse this field?

Thanks in Advance
John
 
John, you have given the separators as "," and " " in the strtok(). That is why you did not get the expected result.
strtok() function accepts a single character delimiters.

Write your own program using strstr() to do the name separation.
 
Hi

Small problem. The delimiter you have specified is ", ".
It is a comma and a space both are taken as delimiters so when you have a word like LONG ISLAND it is taking them as two tokens.
Use
char seps[] = ",";

It will work fine...

 
If these suggestions don't work I have a string splitting
function that does what you ask and returns an array of
the splits.

If you want I'll post it.
 
marsd,
Yes, I'm still having a problem parsing thru my string.
Since my data was entered as: comma then space, it's a problem.
For example, I have JONES, SMITH, ALLEN, PARSON, ...

If you could post that string splitting function, I would appreciate it.

Thanks

John
 
Hi jtrapat1

You could try to "strip" the result of strtok of its spaces (at the front and the back).
 
Thanks for the posts.
I finally fixed the problem.
Had to trim the white space from both ends of the tokens.
Here's the function I ended up using:
------------------------------
char* strtrim(char* str) {
int i = 0;
int j;
while (str!= '\0' && str == ' ')
i = i + 1;
j = i;
while (str[j]!= '\0' && str[j] != ' ')
j = j + 1;
str[j] = '\0';
return &str;
}
------------------------------

Thanks Again.
John
 
Here they are.
Your method looks like it's better in any case.
I hacked them up so don't expect a lot.


char **retSplit(char *str, char sep) {
int cur = 0, acnt = 0, p, len;
char **new = NULL;
char *ppt = str,*tpt;


/*init*/
len = strlen(str);
if ( (p = numSeps(str,sep)) > 0) {
ppt = str;
new = malloc(p * sizeof(char *));
if (!new) {
return NULL;
}
}

/* main work here*/
p = 0;
while (*str) {
cur++;
if (*str == sep) {
printf("Found separator at: %d\n String is: %s\n",cur,ppt + cur);
new[acnt] = malloc((cur - p) + 1 * sizeof(char));
/*new string boundary*/
tpt = ppt + p;
printf("New string marker: %s\n", tpt);
strncpy(new[acnt],tpt,(cur - p) - 1);
termStr(new[acnt]);
p = cur;
acnt++;
}
*str++;
}

/*final cut*/
tpt = ppt + p ;
new[acnt] = malloc((len - p) + 1 * sizeof(char));
strncpy(new[acnt],tpt,(len - p));
/*return the resulting array*/
return new;
}

NumSeps() just goes through the char array looking
for sep and returns the number of matches + 1 for the
array allocation.

int numSeps(char *str, char sep) {
int m = 0;

while (*str) {
if (*str == sep) {
m++;
}
*str++;
}
return m + 1;
}

termStr() takes a char array, reallocs the array size + 1
and terminates len + 1. Like strtok it modifies the
original char array.
void termStr(char *str) {
int m = 0;
char *sp = str;
while (*sp++) {
m++;
}
realloc(str,(m + 1) * sizeof(char));
str[m + 1] = '\0';
}

Sample run without debugging printf's reading from
stdin.
>tstr "this, is a tes,tof the,emergency"
this at 0
is a tes at 1
tof the at 2
emergency at 3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top