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);
}