Solved

C Programing - get substring between two chars

Posted on 2008-10-21
6
1,108 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
  • 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 500 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 nested-loops 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.

708 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

16 Experts available now in Live!

Get 1:1 Help Now