How to create recursive implementation of atoi() in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
 
int atoiRecursively(char *str, int n) {
    if (n == 1)
        return *str - '0';
  
    return 10 * atoiRecursively(str, n - 1) + (str[n - 1] - '0');
}
  
int main()
{
    char str[] = "8072";
     
    int n = atoiRecursively(str, strlen(str));
     
    printf("%d", n);
     
    return 0;
}
 
 
 
 
/*
run:
      
8072
 
*/

 



answered Nov 24, 2023 by avibootz
edited Nov 24, 2023 by avibootz
...