Sunday, 20 March 2016

C Program To Convert String To Integer Without Using Library Functions

#include<stdio.h>
#include<string.h>
int strToInt (char[] );

int main()
{

    char str[10];
    int intNum;

    printf("Enter any integer as a string: ");
    scanf("%s",str);

    intNum = strToInt(str);

    printf("Equivalent integer value: %d",intNum);

return 0;
}

int strToInt (char str[])
{

    int i=0,sum=0;

    while(str[i] != '\0')
{

        if(str[i] < 48 || str[i] > 57)
{
            printf("Can't convert it into integer.\n");
            return 0;
        }
        else
{
            sum = sum*10 + (str[i] - 48);
            i++;
        }
    }

return sum;

}

Output:-


No comments:
Write comments

Recommended Posts × +