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

In this tutorial, I am going to teach you “C Program to Convert String to Integer Without Using Library Function atoi()”. Full source code of the concept is given below.

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

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

Program Code

#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("nnEnter Integer Number: ");
    scanf("%s",str);

    intNum = strToint(str);

 if(intNum==0)
 {
 printf("nEnter The Number Not Stringnn");
 }
 else
    {
     printf("nnEquivalent Integer Value: %d",intNum);
 }
 }
 
return 0;
}

int strToint (char str[])
{

    int i=0,sum=0;

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

        if(str[i] < 48 || str[i] > 57)
   {
            printf("nnCan't Convert Into Integer");
            return 0;
        }
        else
   {
            sum = sum*10 + (str[i] - 48);
            i++;
        }
    }

return sum;

}

return sum;

}

Final Words

I hope this article helps you to learn “C Program to Convert String to Integer Without Using Library Function atoi()“. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment