Solved

C Programing - get substring between two chars

Posted on 2008-10-21
6
1,131 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

770 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