[2 Methods] C Program to Implement atoi() Function | Convert String to Int

[2 Methods] C Program to Implement atoi() Function | Convert String to Int

The string is nothing but a group of characters.

String Example: "abcd"

These characters can also be the numbers.

String Example (consist of numebrs): "1234"

If you add the double quotation to the number, it becomes the string. Even if it contains the number values, you can not perform any mathematical operation. The compiler considers it as a pure string, not a number.

Many times in your project you need to convert this string to a number so that you can perform mathematical calculations.

You can convert the string to an integer manually or you can also use the inbuilt function atoi(). Before going to the inbuilt function, we write our code.

Or in interviews, you may be asked to write a C Program to implement atoi() function.

Converting String to Int in C code

1. C Program to Implement atoi() Function:

We read each character from the string and based on the digit position on the character in the string, we will multiply by a power of ten.

Let’s take an example. We want to convert the string “354” into an integer value as 354.

Read 3rd character:
4 * 10^0 = 4

Read 2nd character
5*10^1 = 50

Read 1st character
3*10^2 = 300

Calculate the sum of all numbers
4+50+300 = 354

We read the characters from the string in reverse order. To read the last character first, you need to find out the number of characters in the string for which you can use the function strlen().

Prerequisite:

strlen() function in C – It returns the number of characters in the given string.

ASCII value of the integer character. You can refer to the ASCII value in the table below.

Character ASCII Value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57

Program to write to your own atoi() function:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
    char strNum[100] = {0};
    int nVal = 0, i, j, len;
    printf("Enter a number string: ");
    scanf("%s",&strNum);
    printf("\nYour string is %s", strNum);
  
    len = strlen(strNum);
    int nDecCount = 1;
  
    for(int i=len-1; i>=0; i--){
        nVal = nVal + nDecCount * ( strNum[i] - '0' );
        nDecCount *=10;
    }
    printf("\nYour integer value is %d", nVal);
    return 0;
}

Output of the Program:

Enter a number string: 1453
Your string is 1453.
Your integer value is 1453

You can perform any mathematical operation on this result integer value.

2. Convert String to Int Using Built-in Function atoi()

If you don’t want to do all cumbersome operations on the string, you can simply use in build function atoi() to convert the string into an integer value.

Syntax of atoi():

int atoi(const char *str)

It takes the string as an input value and gives the corresponding integer value as an output.

The functionatoi() is available in stdlib.h header file. So, you need to include it in your program.

Program using atoi():

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char strNum[100] = {0};
    int nVal = 0;
    printf("Enter a number: ");
    scanf("%s",&strNum);
  
    nVal = atoi(strNum);
    printf("%d", nVal);
    return 0;
}

If you are working on any project, you are free to use an inbuilt function rather than doing it manually. But many times, the interviewer asks you to implement a manual function to test your logic-building skill.

Other String-related C Programs you can try:

This is all about the C Program to Implement atoi() function. There can be multiple methods to do the same. If you have any other methods, write in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *