code comité 100-atoi

PHOTO EMBED

Tue Jun 27 2023 00:50:43 GMT+0000 (Coordinated Universal Time)

Saved by @thefirewall #html

#include <stdio.h>
/**
 * _atoi - converts string to integer
 *@s: pointer to char
 * Return: int
 */

int _atoi(char *s)
{
int res = 0;
int nb  = 0;
int cpt = 0;
int i =0;
for (i = 0; s[i] != '\0' ; ++i)
{
if (s[i] == '-')
nb++;
if (s[i] >= '0' && s[i] <= '9')
{
cpt++;
res = res * 10 + s[i] - '0';
}
if (cpt >= 1 && !(s[i] >= '0' && s[i] <= '9'))
break;
}
if (nb % 2 != 0)
{
res = res * -1;
}
return (res);
} /*end method*/
content_copyCOPY