[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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