Q:

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

belongs to collection: String in C Programs

0
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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

int strToint (char[]);

int main()
{
   
    printf("=====================================");
    printf("\nnerdutella.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:

nerdutella.com

======================================

Enter Integer Number 123

Equivalent Integer Value:123

Enter Integer Number :102

Equivalent Integer Number abc

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to Convert String Lowercase to Uppercase... >>
<< C Program to Delete a Substring From a String [Upd...