Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Seperation using delimiters

Posted on 1998-08-29
13
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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 how to create, access, and change arrays in the C programming language.
Suggested Courses

618 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