Monday, May 27, 2013

convert a numerical string into a number

here is an example to convert a string of numbers into a number it represents

// a program to convert given string into integer ex: "124" to 124

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char str[10] ;
    int i,l ,n , num =  0;

    printf("Enter the number to be converted :");
    scanf("%s",str);

    l = strlen(str);

    for(i = 0 ; i < l ; i++)
    {
           n = str[i]-48;
           num = num +n*pow(10,l-i-1);
    }

    printf("num : %d",num);

}