Implement atoi() function in C | Iterative & Recursive
Write an efficient function to implement atoi() function in C. The standard atoi() function converts input C-string to its corresponding integer value.
Iterative Implementation of atoi():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> // Iterative function to implement `atoi()` function in C long atoi(const char* S) { long num = 0; int i = 0; // run till the end of the string is reached, or the // current character is non-numeric while (S[i] && (S[i] >= '0' && S[i] <= '9')) { num = num * 10 + (S[i] - '0'); i++; } return num; } // Implement `atoi()` function in C int main(void) { char S[] = "12345"; printf("%ld ", atoi(S)); return 0; } |
Output:
12345
Recursive Implementation of atoi():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <string.h> // Recursive function to implement `atoi()` function in C long atoi(const char *S, int n) { // base case – the end of the string is reached, or the // current character is non-numeric if (n < 0 || (S[n] < '0' || S[n] > '9')) { return 0; } // recur for remaining digits and append the current digit // to result of recursion multiplied by 10 return (10 * atoi(S, n - 1) + (S[n] - '0')); } // Implement `atoi()` function in C int main(void) { char S[] = "12345"; printf("%ld", atoi(S, strlen(S) - 1)); return 0; } |
Output:
12345
The above iterative and recursive implementations of atoi() is not similar to the standard implementation of atoi(). The function should first discard as many whitespace characters until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign, followed by as many base-10 digits as possible, and interprets them as a numerical value. The string, if it contains any additional characters after those that form the integral number, are ignored. No conversion is performed if the string is empty or only contains whitespace characters and zero is returned. Following atoi() implementation takes care of all these:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <stdio.h> // Iterative function to implement `atoi()` function in C long atoi(const char S[]) { long num = 0; int i = 0, sign = 1; // skip white space characters while (S[i] == ' ' || S[i] == '\n' || S[i] == '\t') { i++; } // note sign of the number if (S[i] == '+' || S[i] == '-') { if (S[i] == '-') { sign = -1; } i++; } // run till the end of the string is reached, or the // current character is non-numeric while (S[i] && (S[i] >= '0' && S[i] <= '9')) { num = num * 10 + (S[i] - '0'); i++; } return sign * num; } // Implement `atoi()` function in C int main(void) { char S[] = " -1234567890"; printf("%ld ", atoi(S)); return 0; } |
Output:
-1234567890
The time complexity of all the above solutions is O(n), where n is the length of the input string.
That’s all about atoi() implementation in C.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)