
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write Your Own Atoi in JavaScript
In C programming language we have a function that takes a single string or character array as the parameter and returns an integer that may be represented by the given string, if the current string is invalid then it reads only up to the first valid index and will return that value. We will see the complete code with the explanation.
Sample Examples
Input 1
string S = "-9845"
Output 1
-9845
Explanation
We are given a string that represents a number so we have just got the same output.
Input 2:
string str = "90 uy78"
Output 2
Invalid Input
Explanation
The given string is not a valid integer as it contains the lowercase English characters, and whitespaces. Therefore, we have given the output according to it.
Input 3
string str = "539"
Output 3
539
String is Valid
We have seen the examples above, now let us move to the steps of the implementation.
First, we will create a function that will take the string as the input and will return the integer as the return value.
In the function, first we will create a function to check or store that the given number is negative number or not by checking the first character of the string.
If the first character is the minus sign, then we will traverse the string from the 1st index otherwise from the zeroth index.
We will create a variable to store the result and initialize it will zero.
At each iteration, we will multiply the current integer by 10 and then add the current digit to it.
To convert the string digit to an integer value we will use the function parseInt(), which takes the character or string as the input and given the integer value as the output.
At the end, we will return the final answer and later will print it.
Example
// function to convert the string to an integer function atoi(str){ // Assuming the string is valid var neg = 1 // checking for the negative number if(str[0] == '-'){ neg = -1 } var ans = 0; var i = 0; // if the number is the negative number then start from the next index if(neg == -1){ i = i + 1 } while (i < str.length){ ans = ans * 10 + parseInt(str[i]); i = i + 1; } ans = ans* neg return ans; // returning the answer } // defining the input and calling the function str = "-4578038"; // calling the function var ans = atoi(str); // printing the answer console.log("The value of the current number is: " + ans);
Output
The value of the current number is: -4578038
Time and Space Complexity
The time complexity of the above code is O(N), where N is the number of characters in the given string.
The space complexity of the above code is O(1), as we are not using any extra space.
String Can be invalid
We will use the previous code for most of the functions but the main thing is we have to check if the given string is valid or not. If the string is invalid then we have to find that and for that we will create a function that will take a single character as parameter and returns boolean value.
We will check if the string contains any whitespace or other character which is not the digit by this function.
Example
// function to check if the current character is digit or not function check(cha){ for(var i = '0'; i <= '9'; i++){ if(cha == i){ return true; } } return false; } // function to convert the string to an integer function atoi(str){ // Assuming the string is valid var neg = 1 // checking for the negative number if(str[0] == '-'){ neg = -1 } var ans = 0; var i = 0; // if the number is the negative number then start from the next index if(neg == -1){ i = i + 1 } while (i < str.length){ // checking for the invalid case if(check(str[i]) == false){ console.log("The given string represents the invalid number"); return; } ans = ans * 10 + parseInt(str[i]); i = i + 1; } ans = ans* neg // printing the answer console.log("The value of the current number is: " + ans); } // defining the input and calling the function str = "0987653"; // calling the function atoi(str);
Output
The value of the current number is: 987653
Time and Space Complexity
The time complexity of the above code is O(N), where N is the number of characters in the given string.
The space complexity of the above code is O(1), as we are not using any extra space.
Conclusion
In this tutorial, we have implemented a JavaScript program to convert the number present in the form of a string to an integer. We have traversed over the string and checked if the current string represents a valid number or not. We have created a function, that will detect that the current character of the string is digit or not.