0

I have read this question ( Convert a string into an int ) before posting but I still have doubts.

I am very new to C and I'am trying to write a function which takes a string as parameter , something like this :

  • "99Hello" or "123ab4c"

And then returns the first the numbers it found into an int. So the result would be :

  • 99 or 123

I'am trying to do this by finding the characters in the string(which correspond to numbers in the ASCII table) and then store them in the int I have created and returned by the function.

I'am having trouble though because when I find those character ( which correspond to numbers) I don't know how to save them to an int data type variable or how to convert them. I know there is a function called atoi which does this automatically but I would like to know how to do this by myself.

This is what I wrote so far :

int ReturnInt(char *str)
{
    int     i;
    int     new;

    i = 0;
    new = 0;
    while (str[i] != '\0')
    {
         if( str[i] >= '0' && str[i] <= '9')
        {
            str[i] = new - '0';
        }
        i++;
    }

    return (new);
}

izzypt
  • 170
  • 2
  • 16
  • 1
    If you have the digits 3, 5, and 7, and you look at them one at a time, you see “3”, then “35”, then ”357”. If you are building a number as you look at each digit, what numbers do you build along the way? How do you get from one number to the next number? – Eric Postpischil Dec 12 '20 at 14:59
  • 1
    Also, never write `new - 48` to get the value of a digit from its character code. Write `new - '0'`. You should avoid using “special” numbers in programs. The compiler will replace `'0'` with the code it uses for the character 0. That code can be different in different C implementations, so `'0'` will always be correct, whereas 48 will not always be correct. – Eric Postpischil Dec 12 '20 at 15:00
  • Thanks @Eric , I will fix that 48. About your first question , I will keep thinking about it ! – izzypt Dec 12 '20 at 15:03

2 Answers2

2

I think you should be able to form the desired integer from digits by multiplying them by 10 and adding them while iterating. Moreover, I think your loop's terminating condition should also include isDigit check

For example, for input 99Hello

i = 0, new = 0 -> new = 0 * 10 + 9 (9)

i = 1, new = 9 -> new = 9 * 10 + 9 (99)

i = 2, condition not satisfied as it's not a digit

int ReturnInt(char str[]) {
  int i;
  int new;

  i = 0;
  new = 0;
  while (str[i] != '\0' && (str[i] >= '0' && str[i] <= '9'))
    {
      int currentDigit = str[i] - '0';
      new = new * 10 + currentDigit;
      i++;
    }

  return (new);
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

You just have to take a place holder , and that has to be multiplied by 10 for every iteration so that we get 10, 100, 1000 place holders and we can add that quantity to remaining numbers.

#include <stdio.h>
#include <ctype.h>

int str2num(char*strnum);

int main()
{
    char *num1 = "122AB";
    char *num2 = "2345AB9C";
    char *num3 = "A23AB9C";
    
    printf("num1 = %d\n", str2num(num1));
    putchar('\n');
    printf("num2 = %d\n", str2num(num2));
    putchar('\n');
    printf("num3 = %d\n", str2num(num3));
    
    return 0;
}

int str2num(char*strnum)
{
    int num = 0;
    int ph = 10;// for getting tens,100s etc 
    
    while(strnum && *strnum && isdigit(*strnum))
    {

num = num * ph + ( (*strnum) - '0'); strnum++;

    }
    return num;
}
IrAM
  • 1,720
  • 5
  • 18