Solved

Seperation using delimiters

Posted on 1998-08-29
13
345 Views
Last Modified: 2010-04-15
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
Comment
Question by:singhtaj
13 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1252424
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
 
LVL 11

Expert Comment

by:alexo
ID: 1252425
Another option is to use scanf() with sets ([...] and [^...]).
0
 

Author Comment

by:singhtaj
ID: 1252426
How do I use strtok in this case ? I tried but am unsuccessful...

0
 

Author Comment

by:singhtaj
ID: 1252427
How do I use strtok in this case ? I tried but am unsuccessful...

0
 
LVL 84

Expert Comment

by:ozo
ID: 1252428
#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
 

Author Comment

by:singhtaj
ID: 1252429
How do I use strtok in this case ? I tried but am unsuccessful...

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 84

Expert Comment

by:ozo
ID: 1252430
/*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
 

Author Comment

by:singhtaj
ID: 1252431
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
 
LVL 84

Expert Comment

by:ozo
ID: 1252432
it should work.  what does your code look like?
0
 

Author Comment

by:singhtaj
ID: 1252433
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
 

Author Comment

by:singhtaj
ID: 1252434
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
 
LVL 84

Accepted Solution

by:
ozo earned 60 total points
ID: 1252435
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
 

Author Comment

by:singhtaj
ID: 1252436
Thanks for the help

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now