Link to home
Start Free TrialLog in
Avatar of shanikawm
shanikawmFlag for Sri Lanka

asked on

C Programing - get substring between two chars

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

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

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can use strtok to tokenize the string, and then use atoi to convert each token to its corresponding integer value.
Avatar of divyeshhdoshi
divyeshhdoshi

char str1[] = "455:2355;";


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

@divyeshhdoshi : this is about C ;)
Avatar of shanikawm

ASKER

Great. Thanks.