Solved

Seperation using delimiters

Posted on 1998-08-29
13
361 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 158
Want to delete all my personal data 13 146
Super Scope, DHCP 5 91
A  &&  B  || (C||D) how it works in c 7 99
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

840 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