Skip to main content

Command Palette

Search for a command to run...

Implement Atoi

Published
2 min read
D

Hello everyone, I'm Dhanraj, a CSIT Btech student currently pursuing my degree at Rajarambapu Institute of Technology. I'm a passionate web developer and Java programmer, with a keen interest in data structures and algorithms.

Programming has always been my passion since my school days, and I have been constantly exploring the world of computer science. My fascination with web development began when I discovered the power of the internet, and since then, I have been honing my skills to create beautiful, functional websites.

I believe that perseverance and hard work are the keys to success in any field, and programming is no exception. I enjoy the challenge of solving complex problems and take pride in delivering high-quality code that meets the requirements of my clients.

Apart from programming, I also have a keen interest in playing chess and expanding my knowledge of the stock market. I strongly believe that a well-rounded personality is crucial for success in life, and I always strive to balance my academic pursuits with my extracurricular activities.

I'm excited to share my experiences and knowledge with you through my blog. Whether you're a beginner or an experienced programmer, I hope that my articles will inspire you to push your limits and reach new heights in your programming journey.

Hello coders I'm Dhanraj. In this post, I will show you how to implement our own atoi function in Java. This function can convert a string to an integer, even if the string contains a negative sign. It also returns -1 if the string has any non-digit characters. Let's take a look at the following simple code and follow me for more interesting content.

Your task is to implement the function atoi. The function takes a string(str) as argument and converts it to an integer and returns it.

Note: You are not allowed to use inbuilt function.

Example 1:

Input:
str = 123
Output: 123

Example 2:

Input:
str = 21a
Output: -1
Explanation: Output is -1 as all
characters are not digit only.

Your Task:
Complete the function atoi() which takes a string as input parameter and returns integer value of it. if the input string is not a numerical string then returns -1.
Note: Numerical strings are the string where either every character is in between 0-9 or where the first character is '-' and the rest all characters are in between 0-9.

Expected Time Complexity: O(|S|), |S| = length of string str.
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ length of S ≤ 10

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Solution:

class Solution
{
    int atoi(String str1) {

        int res = 0;

        char str[] = str1.toCharArray();
        int sign = 1;
        int i = 0;

        if (str[0] == '-') {
            sign = -1;
            i++;
        }
        for (; i < str.length; ++i)
        {
            if(0<=str[i]-'0' && str[i]-'0'<=9 )
                res = res * 10 + str[i] - '0';
            else
            {
                return -1;
            }
        }

        return sign * res;
    }
}

Output:

For Input: 
123
Your Output: 
123
Expected Output: 
123

More from this blog

DHANRAJ NIKAM's blog

7 posts