Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C Programing - get substring between two chars

Posted on 2008-10-21
6
Medium Priority
?
1,169 Views
Last Modified: 2010-04-21
I have  strings like follow.

 char str1[]="455:2355;";
 char str2[]="317:5569;";
 char str2[]="213:8254;";
.....

What is the easiest way to get the integer value between ":" and ";"?
I could get the first one (i.e. 455,317,213 ...) using atoi(str1).
I'm using gcc version 3.3.2

0
Comment
Question by:shanikawm
[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
  • 4
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22765582
If the format of your strings will always be like that, then something like this should work :
char str1[] = "455:2355;";
int value = 0;
 
char *pos = strstr(str1, ':');
if (pos) {
  value = atoi(pos + 1);
}

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 22765597
Or you can use strtol to retrieve both values :
char str1[] = "455:2355;";
char *pos = 0;
long value = 0;
long value2 = 0;
 
value = strtol(str1, &pos, 10);
/* pos now points to the ':' character - you can check this to make sure */
value2 = strtol(pos + 1, &pos, 10);
/* pos now points to the ';' character - you can check this to make sure */

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22765600
Or you can use strtok to tokenize the string, and then use atoi to convert each token to its corresponding integer value.
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!

 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22765608
char str1[] = "455:2355;";


string result=str1.substring(str1,str1.charat(':') , str1.charat(';') - str1.charat(':') );
convert result to integer

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22765611
@divyeshhdoshi : this is about C ;)
0
 
LVL 8

Author Closing Comment

by:shanikawm
ID: 31508191
Great. Thanks.
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.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

705 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