Solved

C Programing - get substring between two chars

Posted on 2008-10-21
6
1,141 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

828 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