• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Seperation using delimiters

Hi,

If I have a string like
"ABC_DEF_123"

How would I seperate the three of them ?
what I want is the three of them seperated for example
after seperation :
temp1 will contain "ABC"
temp2 will contain "DEF"
and temp3 will contain "123"

How would I do this ? I tried using standard stuff but still
am unsuccessful ...

Any help will be appreciated.
Thanks
0
singhtaj
Asked:
singhtaj
1 Solution
 
MikeP090797Commented:
Use strtok inside a loop:

The first call is done outside the loop when the first param is the string you want to parse.
Then you just loop for as long it returns something, whith the first parameter set to NULL
0
 
alexoCommented:
Another option is to use scanf() with sets ([...] and [^...]).
0
 
singhtajAuthor Commented:
How do I use strtok in this case ? I tried but am unsuccessful...

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
singhtajAuthor Commented:
How do I use strtok in this case ? I tried but am unsuccessful...

0
 
ozoCommented:
#include <string.h>

char *string="ABC_DEF_123";
char *temp1,*temp2,*temp3;
temp1=strtok(string,"_");
temp2=strtok(0,"_");
temp3=strtok(0,"_");
printf("trmp1=%s, trmp2=%s, temp3=%s;\n",temp1,temp2,temp3);
0
 
singhtajAuthor Commented:
How do I use strtok in this case ? I tried but am unsuccessful...

0
 
ozoCommented:
/*Another option is to use*/
char tmp1[100],tmp2[100],tmp3[100];
sscanf("ABC_DEF_123","%99[^_]_%99[^_]_%99[^_]",tmp1,tmp2,tmp3);
printf("tmp1=%s, tmp2=%s, tmp3=%s;\n",tmp1,tmp2,tmp3);
0
 
singhtajAuthor Commented:
ozo your "sscanf" solution is not working !!!
I wonder what is wrong with it ???
Basically the strings printed out have nothing in them...
Any ideas ???

0
 
ozoCommented:
it should work.  what does your code look like?
0
 
singhtajAuthor Commented:
Okay I am sorry!!!

Its working. It was my error internally.
Now How do I grade you ? Because Now I don't have any options
of giving you the grade ?
0
 
singhtajAuthor Commented:
By the way ozo, is it possible to scan the last item "123"
to be an integer ?

That is instead of char tmp3[100] being "123"
i want int tmp3=123 ?

Is this possible using sscanf ?


0
 
ozoCommented:
char tmp1[100],tmp2[100];
int tmp3;
sscanf("ABC_DEF_123","%99[^_]_%99[^_]_%d",tmp1,tmp2,&tmp3);
printf("tmp1=%s, tmp2=%s, tmp3=%d;\n",tmp1,tmp2,tmp3);
0
 
singhtajAuthor Commented:
Thanks for the help

0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now