Idanref

Turn char digits to integers in C

Nov 16th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int make_it_three(char c1, char c2, char c3)
  5. {
  6.     int d1, d2, d3, result = 0;
  7.  
  8.     if((c1 >= 48 && c1 <= 57) && (c2 >= 48 && c2 <= 57) && (c3 >= 48 && c3 <= 57))
  9.     {
  10.  
  11.         for(int i = 48; i <= 57; i++)
  12.         {
  13.             if (c1 == i)
  14.             {
  15.                 d1 = i - 48;
  16.                 break;
  17.             }
  18.         }
  19.  
  20.         for(int j = 48; j <= 57; j++)
  21.         {
  22.             if (c2 == j)
  23.             {
  24.                 d2 = j - 48;
  25.                 break;
  26.             }
  27.         }
  28.  
  29.         for(int k = 48; k <= 57; k++)
  30.         {
  31.             if (c3 == k)
  32.             {
  33.                 d3 = k - 48;
  34.                 break;
  35.             }
  36.         }
  37.  
  38.        
  39.         result += d1;
  40.         result = (result * 10) + d2;
  41.         result = (result * 10) + d3;
  42.  
  43.  
  44.         return result;
  45.  
  46.     }
  47.  
  48.  
  49.     return 0; // If all three chars are not digits.
  50.        
  51. }
  52.  
  53.  
  54. void main()
  55. {
  56.     char c1 = '1', c2 = '9', c3 = '3';
  57.  
  58.     int result = make_it_three(c1, c2, c3);
  59.  
  60.     printf("%d\n", result);
  61. }
Add Comment
Please, Sign In to add comment