Sunday, 20 March 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

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment