type conversion(string to int)

can anybody suggest how to convert string to integer?
exactly what i mean is ,say we have,
sdf589fsd6985sd2dd
and i want to extract only digits and convert to int as to perform arithmatic operation.how to extract and convert?

Comments

  • : can anybody suggest how to convert string to integer?
    : exactly what i mean is ,say we have,
    : sdf589fsd6985sd2dd
    : and i want to extract only digits and convert to int as to perform arithmatic operation.how to extract and convert?
    :
    [blue]you are going to have to parse that string one character at a time. If the character is a digit then add it to another string. When done, you can use atoi() to convert the second string to an integer. Use the macro isdigit() to find out if a character is a digit or not.

    Another method that doesn't involve creating another string is to convert the digits to binary and add them to the int variable.
    [code]
    int n = 0;
    char str[] = "sdf589fsd6985sd2dd";
    //convert the 4th character (the first '5') to int
    if( isdigit(str[3]) )
    n = (n * 10) + str[3] - '0';
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion