#Custom Atoi Function

11 messages · Page 1 of 1 (latest)

topaz elbow
#

My code isn't giving the output I want.. I want to convert a string to a number and when it encounters any other special character apart from numeric values, it should stop and print out the digit. But even when I add a break statement, it just returns 0.

#include <stdio.h>

int _atoi(char *s)
{
        int i = 0;
        int sign;
        int result;
        result = 0;
        sign = 1;

        for (i = 0; s[i] != '\0'; i++)
        {
                if (s[i] == '-')
                {
                        sign = -1;
                        i++;
                }
                else if (s[i] == '+')
                {
                        sign = 1;
                        i++;
                }

                else if (s[i] == ' ' || s[i] == '\t')
                {
                        i++;
                }


                if (s[i] >= 48 && s[i] <= 57)
                {
                        result = result * 10 + s[i] - 48;
                }

        }

        return (sign * result);
}

int main (void)
{
        int nb;
        nb = _atoi("---++++ -++ Sui - te -   402 #cisfun 11 :)");
        printf("%d\n", nb);
        return (0);
}

It's supposed to print only 402, but it prints 11 after wards.
This is the code. Just like a custom atoi function.

shell bladeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

gleaming prawn
#

it seems that you don’t exit the loop when you encounter a char that isn’t a digit after the conversion of 402

#

it only exit at the end of your string so it keeps looping and looping, that’s why it’s printing 11 after

#

(moreover, be careful about the way you deal with your sign variable :) )

last ingot
# topaz elbow My code isn't giving the output I want.. I want to convert a string to a number ...
  1. The way you set your sign variable doesn't make sense. So if there's +-4, then what you apparently want to do is then set the sign to 1 for the + and then to -1 for the -, which makes sense as the mathematical value is -4, but if you have -+ then mathematically it'd be the same number, however in your program you would set the sign first as -1 for the -, then to 1 for the +, meaning your program would wrongly see it as a +4.

  2. Why is there an i++ in all the sign cases? You're incrementing i at the end of the loop anyways, all this is doing is just causing you to skip some characters.

  3. There is never any check for characters apart from numerical values.

  4. There is never any check for integer overflows (although I don't think you need to worry about that here)

  5. Instead of hardcoded integer literals like 48 and 57 you should use char literals like '0' and '9'

topaz elbow
#

Okay so how do I go about the incrementation. How do I check for characters too?

last ingot
# topaz elbow Okay so how do I go about the incrementation. How do I check for characters too?

For checking what character you're currently reading I would recommend writing 3 functions is_digit, is_sign and is_whitespace. Could look like so:

int is_digit(char c) {
    // returns 1 if the character c is a digit, 0 otherwise
    return '0' <= c && c <= '9';
}

int is_whitespace(char c) {
    // returns 1 if the character c is a whitespace character of some sort, 0 otherwise
    return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v';  // the latter 2 are the less known "form feed" and "vertical tab"
}

int is_sign(char c) {
    // returns -1 if it's a '-', 1 if it's a '+' and 0 if it's neither
    if (c == '-')
        return -1;
    else if (c == '+')
        return 1;
    else
        return 0;
}

Using these 3 functions you should have an easier time structuring the rest of your code

gleaming prawn
#

it’s maybe not helping that you’re giving the full answer

last ingot