Solved

C Programing - get substring between two chars

Posted on 2008-10-21
6
1,121 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Retrun object in plist format 5 55
C#, VS15, StructLayout 1 122
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 60
IIS Log files on Exchange 2013 server 6 85
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 and use structures 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.

867 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

20 Experts available now in Live!

Get 1:1 Help Now