C Program To Write Your Own atoi()


The aim of this article is to implement a program C Program To Write Your Own atoi().

Before we begin, let us take a deeper understanding of what an atoi() function is all about. This will help in writing the program as well as understanding the concepts pretty much easier. To the ones who are not much aware of what an atoi() function is. Here you go.

The atoi() function changes a string of elements into an integer value. The string that is entered is a string of characters that has the potential to become a numeric value of the return type. The first character that the function determines is not a component of a number causes it to stop reading the input string. The string's final null character could be the culprit. Exponents nor decimal numbers cannot be used with the atoi() function.

The string argument s is converted to an integer via the C library's int atoi(const char *s) function. The function typically turns a string parameter into an integer. All the empty white space characters at the very start of the string are skipped by the atoi() function.

Also note that the conversion process ends when it encounters the first character that is not a number.

Approach

The approach to solve this problem and get a C program to write your own atoi() is explained below.

Before that let us see the syntax to write the atoi function.

The syntax is −

int atoi(const char *s) 

Where, s is the string.

The function receives one parameter, s, which designates the string argument which to be transformed into its equivalent integer value.

Return Value − The function returns an equivalent integer number of the provided string number if s is a valid input. The function returns 0 if there is no valid conversion.

Generally speaking, the following parameters can be used to effectively convert strings to numbers −

  • '0123456789' and other strings are composed entirely of ASCII digits.

  • strings with the '+' character at the start and only using ASCII characters

  • strings that start with the letter '-' and are totally made up of ASCII digits.

Also you should know that the Atoi() function operates in stages. That is, let me explain it to you in very simple terms. The atoi() function creates strings that represent integers one at a time. When a non-ASCII character is encountered, the function only fails.

After breaking, the Atoi() method outputs one of two potential values. When not any values have been changed and the function is invoked at the beginning of the string, it returns 0. In the absence of such, it will provide the most current number it possesses.

Algorithm

The algorithm to obtain a C program to write your own atoi() given below −

  • Step 1 − Define an integer variable in order to store the integer value of the strinf. Here we declared the variable name as 'intValue'.

  • Step 2 − Define a string s.

  • Step 3 − Define atoi function.

  • Step 4 − pass the string value.

  • Step 5 − print the integer value as the output.

Example: C program

Here is the C program implementation of the above written algorithm to obtain a C program to write your own atoi() −

#include <stdio.h>
#include <stdlib.h>
int main() {
   int intValue;
   char s1[] = "98765";
   intValue = atoi(s1);
   printf("String value = %s 
", s1); printf("Integer value = %d
",intValue); char s2[] = "TutorialsPoint"; intValue = atoi(s2); printf("String value = %s
", s2); printf("Integer value = %d
", intValue); return 0; }

Output

On execution, it will produce the following output −

String value = 98765
Integer value = 98765
String value = TutorialsPoint
Integer value = 0

Conclusion

Likewise we can obtain a C program to write your own atoi(). The challenge of obtaining the C program to write your own atoi() is well resolved in this article.

Here C programming code as well as the algorithm and the methodology to obtain a C program to write your own atoi() are provided.

Updated on: 30-Oct-2023

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements