Advertisement
brandonmunda1

Print an Integer (100-atoi.c)

Mar 1st, 2023
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. /**
  4.  * _atoi - converts a string to an integer
  5.  * @s: string to be converted
  6.  *
  7.  * Return: the int converted from the string
  8.  */
  9. int _atoi(char *s)
  10. {
  11.     int i, d, n, len, f, digit;
  12.  
  13.     i = 0; /* an integer to help us iterate in our loop from 0 to len*/
  14.     d = 0; /* checks signage */
  15.     n = 0; /* value of our integer which we keep modifiying char by char, also keeps tracks of the powers of 10, 10th, 100th, 1000th */
  16.     len = 0; /* length of our string */
  17.     f = 0;
  18.     digit = 0;
  19.  
  20.     while (s[len] != '\0') /* while loop to determine length of our string */
  21.         len++;
  22.  
  23.     while (i < len && f == 0)
  24.     {
  25.         printf("\n\n*Value of i is %d*\n\n", i);
  26.         printf("\n\n*Value of f is %d*\n\n", f);
  27.  
  28.         if (s[i] == '-')
  29.         {
  30.             printf("\n\n*Value of s[i] is %c*\n\n", s[i]);
  31.             ++d;
  32.             printf("\n\n*Value of d incremented by 1, d is now %d*\n\n", d);
  33.         }
  34.         if (s[i] >= '0' && s[i] <= '9')
  35.         {  
  36.             printf("\n\n*Value of s[i] is %c*\n\n", s[i]);
  37.             digit = s[i] - '0'; /* the digit at this point is being refered to by it's ascii number (48 - 57), subtracting '0' gives us the actual integer, for example, 9 will be represented as 57, and '0' is 48, thus 57 - 48 will result in the decimal 9 */
  38.             printf("\n\n*digit is (s[i] - '0') = %d*\n\n", digit);
  39.             if (d % 2)
  40.             {
  41.                 printf("\n\n*Value of d is %d*\n\n", d);
  42.                 digit = -digit;
  43.                 printf("\n\n*Signage of digit is changed, value of digit is %d*\n\n", digit);
  44.             }
  45.             n = n * 10 + digit;
  46.             printf("\n\n*Value of n is (n * 10 + digit) = %d*\n\n", n);
  47.             f = 1;
  48.             printf("\n\n*Value of f is set to %d*\n\n", f);
  49.             if (s[i + 1] < '0' || s[i + 1] > '9')
  50.             {  
  51.                 printf("\n\n*If next character (s[%d]is not an integer (not a sign - +), break out of the while loop *\n\n", s[i + 1]);
  52.                 break;
  53.             }
  54.             f = 0;
  55.             printf("\n\n*Value of f is set to %d*\n\n", f);
  56.         }
  57.         i++;
  58.         printf("\n\n*Value of i incremented by 1, i is now %d*\n\n", i);
  59.     }
  60.  
  61.     if (f == 0)
  62.     {  
  63.         printf("\n\n*If there are no numbers in the string and f == %d*\n\n", f);
  64.         return (0);
  65.     }
  66.     printf("\n\n*The integer in string %s is %d*\n\n", s, n);
  67.     return (n);
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement