Atoi in C
A string containing characters is converted into an integer number using the atoi() method. The input is only a string of characters that will be read and stopped when it reaches a character not regarded as part of the number. Additionally, it halts when it comes across the null character at the string's end. It ignores any character in the string that is a blank space.
Syntax:
int atoi(const char *str)
This function accepts one parameter, str, which designates the string argument transformed into its equivalent integer value.
Function Return Value:
This function returns the converted integral number as an int value. It returns 0 if no valid conversion can be made.
The Atoi function requires the below header file.
#include <stdlib.h>
Implementation of atoi () example:
Simple program:
#include <stdio.h>
#include <stdlib.h> //used for atoi function
#include <string.h>
// program for the implementation of atoi in C
int main()
{
int ourvalue;
char sample[10] = "918273645";
ourvalue = atoi(sample);
printf("string value : %s\n", sample);
printf("returned Integer value : %d\n", ourvalue);
char sample1[] = "100ways";
ourvalue= atoi(sample1);
printf("String value : %s\n", sample1);
printf("returned Integer value : %d\n", ourvalue);
char sample2[]="duniyaa";
ourvalue= atoi(sample2);
printf("String value : %s\n", sample2);
printf("returned Integer value : %d\n", ourvalue);
return (0);
}
Output:
Example 2:
In this program, we will see how to handle negative numbers in the atoi function:
For example, if we give an input string as -987.
Then it has to return -987 as our required output.
// here, this is the program if we give a negative number as input for the function
#include <stdio.h>
// A sample atoi() function
int sampleAtoi(char* string)
{
// Initialize ans
int ans= 0;
// First, we Initialize the sign as positive
int init = 1;
// first, we Initialize the index of the first digit
int k = 0;
//We check if the given input is not positive, then we have to change the init to minus
if (string[0] == '-') {
init = -1;
//Then we have to change the index value, also
k++;
}
//now go through all the characters in that
// and change the ans
for (; string[k] != '\0'; ++k)
ans = ans * 10 + string[k] - '0';
// Return product of ans and init
return init * ans;
}
// main function
int main()
{
char string[] = "-918273645";
// Function call
int ourvalue = sampleAtoi(string);
printf("%d ", ourvalue);
return 0;
}
Output:
Example 3:
In this program, we will create our atoi function:
Here it takes only positive numbers.
// Program to create an atoi() in C
#include <stdio.h>
// an atoi() function
int sample1Atoi(char* chocolate)
{
// Initialize the value that is our result as 0
int value = 0;
//Afterward, go through each character in the supplied input to change the value.
//To change the value, we have to multiply it by 10
// and we add then continuously
for (int i = 0; chocolate[i] != '\0'; ++i)
value = value * 10 + chocolate[i] - '0';
// return the value.
return value;
}
//main function
int main()
{
char chocolate[] = "6458";
//call the atoi function
int result = sample1Atoi(chocolate);
printf("%d ", result);
return 0;
}
Output:
Example 4:
Now let's see a last example where atoi can handle white spaces, a sign of the number, invalid input, and overflow.
Run a loop until the character representing the numeral is reached to eliminate the leading whitespaces. If the value is greater than or equal to INT_MAX/10. Next, we return INT_MAX if the value is a positive number and INT_MIN if it is negative.
// implementation of atoi
#include <stdio.h>
#include <limits.h>
int whiteAtoi(const char* ice)
{
int sign = 1, num = 0, k = 0;
//If whitespaces are present, then ignore them.
while (ice[k] == ' ')
{
k++;
}
// sign of the number
if (ice[k] == '-' || ice[k] == '+')
{
sign = 1 - 2 * (ice[k++] == '-');
}
// validate the input
while (ice[k] >= '0' && ice[k] <= '9')
{
// checking overflow condition
if (num > INT_MAX / 10
|| (num == INT_MAX / 10
&& ice[k] - '0' > 7))
{
if (sign == 1)
return INT_MAX;
else
return INT_MIN;
}
num= 10 * num + (ice[k++] - '0');
}
return num * sign;
}
// Driver Code
int main()
{
char ice[] = " -63s89";
// function call
int res = whiteAtoi(ice);
printf("%d ", res);
return 0;
}
Output:
Applications of atoi function:
- It is helpful to validate the input because sometimes we must convert it from a string to an integer.
- It is employed in data processing since the data must be transformed into numerical form before being analyzed.
Conclusion:
- An integer value is created from a string of characters using the C atoi function.
- To utilize the atoi function in C, our program has to contain the stdlib—h header file. The function returns 0 if an invalid string is supplied as an input.
- The atoi() function converts a single string input parameter into an integer value and returns it. The function cannot change the value of the provided string since the input is constant.