Apr 262016

Given a String. Write a function that convert it to number. Such functions are used in larger programs as helper functions.

Input: char* str = "0324";
Output: int 324;


int strToNum(char* str)
    int num = 0;
    for(int i=0; str[i]!='\0';i++)
        int digit = str[i] - '0';
        num = num*10 + digit;
    return num;

The function removes one digit from the string at a time and keep adding it to the number.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>