extract the number from a char string

Hi guys,

i'm new here and new to c/c++ programming.
could you guys guide me how to extract some character/number from a char string?

Example:
char *p = "TAGNAME_C123_V45_S67_M89" or
char *q = "TAG_NAME_C111_V22_S33_M44"

i want to extract the numerical number next to the character 'C', 'V', 'S' & 'M'. The front of the string can be anything and it will end with "_CXXX_VXX_SXX_MXX". if the string does not end with "_CXXX_VXX_SXX_MXX" should return.

Pls help.Thanx a lot
here is what i did
Using sscanf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char *p = "TAGNAME_C123_V45_S67_M89";
const char *q = "EMA_NGAT_C111_V22_S33_M44";

int c, v, s, m;

if(sscanf(p, "%*[^_]_C%d_V%d_S%d_M%d", &c, &v, &s, &m) == 4)
      printf("C=%d\nV=%d\nS=%d\nM=%d\n", c, v, s, m);
else
      printf("This is not a right format");

if(sscanf(q, "%*[^_]_C%d_V%d_S%d_M%d", &c, &v, &s, &m) == 4)
      printf("C=%d\nV=%d\nS=%d\nM=%d\n", c, v, s, m);
else
      printf("This is not a right format");


For the char *p, i manage to get the value, but for the char *q not able to retrieve.
Anybody can point me to the right direction?
If TAGNAME can contain underscores and capital Cs, then you are going to have to use at a minimum a regular expression or else write your own parser. Look up boost::regex at www.boost.org.
Topic archived. No new replies allowed.