3/20/2016

C Program to Convert String to Integer Without Using Library Function atoi()

Problem:- How to convert a string to integer without using library functions in c or Convert String to Integer without library function in C or C program to convert string to integer without using atoi function or How to Convert String to Integer without using any BuiltIn Functions ? or C Program to Convert String to Integer or How to convert string to integer in C or C Program to Convert String to Integer or How to convert INTEGER to STRING without using library functions or C Program which Converts an Integer to String & vice-versa or C Program to Convert String to Integer Without Using Library Function atoi()


Explanation:- here we are converting a string to integer without using an atoi library function, first pass the string to a function and then compare with the first if else condition if condition is true then we can not convert a string into integer and if the condition is false then we can convert a string to integer number. Here we took an input number as a string, not as an integer that is why we have to convert the number to an integer or if we input the string then we cannot convert it.

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.


Solution:-


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

int strToint (char[]);

int main()
{
/*Visit - www.programmingwithbasics.com*/
   
    printf("=====================================");
    printf("\nVisit - www.programmingwithbasics.com");
    printf("\n=====================================");
    
    while(1)
    {
    char str[10];
    int intNum;

    printf("\n\nEnter Integer Number: ");
    scanf("%s",str);

    intNum = strToint(str);

if(intNum==0)
{
printf("\nEnter The Number Not String\n\n");
}
else
    {
    printf("\n\nEquivalent 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("\n\nCan't Convert Into Integer");
            return 0;
        }
        else
  {
            sum = sum*10 + (str[i] - 48);
            i++;
        }
    }

return sum;

}

return sum;

}

Output:-

C Program to Convert String to Integer Without Using Library Function atoi()

You May Also See


1. C Program To Find Area And Circumference Of Circle

2. C Program To Print ASCII Value Of Character

3. C Program To Find Area Of Triangle

4. C Program to Convert a person's name in Abbreviated

5. C Program For Calculate A Simple Interest

No comments:

Post a Comment