Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. /*********************************
  2. * Class: MAGSHIMIM C7            *
  3. * Week 11                        *
  4. **********************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <string.h>
  10.  
  11. #define MAX_LENGTH_OF_INT_NUM 10
  12.  
  13. typedef enum BOOL { FALSE, TRUE };
  14.  
  15. int lengthOfNum(int inputNum);
  16. int reverseNum(int inputNum);
  17. void printProcessedStringNum(int reversedNum, int inputNumLength);
  18.  
  19. int main(void) //Main function
  20. {
  21.     int inputNum = 0, inputNumLength = 0, reversedInputNum = 0;
  22.     inputNum = getInputNum();
  23.     reversedInputNum = reverseNum(inputNum);
  24.     inputNumLength = lengthOfNum(inputNum);
  25.     printProcessedStringNum(reversedInputNum, inputNumLength);
  26.     return 0; //Returning 0
  27. }
  28.  
  29. int getInputNum(void)
  30. {
  31.     int inputNum = 0;
  32.     printf("Enter num: ");
  33.     scanf_s("%d", &inputNum);
  34.     return inputNum;
  35. }
  36.  
  37. void printProcessedStringNum(int reversedNum, int inputNumLength)
  38. {
  39.     int i = 0, copyOfNum = 0;
  40.     char stringProcessedNum[MAX_LENGTH_OF_INT_NUM + 1] = { 0 };
  41.     if (reversedNum < 0)
  42.     {
  43.         stringProcessedNum[0] = '-';
  44.         i = 1;
  45.     }
  46.     else if (reversedNum > 0)
  47.     {
  48.         stringProcessedNum[0] = '+';
  49.         i = 1;
  50.     }
  51.  
  52.     stringProcessedNum[inputNumLength + i] = 0;
  53.  
  54.     for (i; i < inputNumLength; i++)
  55.     {
  56.         while (copyOfNum != 0)
  57.         {
  58.             stringProcessedNum[i] = (char)(copyOfNum % 10 + 48);
  59.             copyOfNum /= 10;
  60.         }
  61.     }
  62.    
  63.     printf("string: %s length: %d\n", stringProcessedNum, inputNumLength);
  64. }
  65.  
  66. int lengthOfNum(int inputNum)
  67. {
  68.     int copyOfNum = 0, inputNumLength = 0;
  69.     copyOfNum = inputNum;
  70.     if (inputNum != 0)
  71.     {
  72.         while (copyOfNum != 0)
  73.         {
  74.             inputNumLength++;
  75.             copyOfNum /= 10;
  76.         }
  77.     }
  78.     else
  79.     {
  80.         inputNumLength = 1;
  81.     }
  82.     return inputNumLength;
  83. }
  84.  
  85. int reverseNum(int inputNum)
  86. {
  87.     int copyOfNum = 0, reversedNum = 0, remainderOfNum = 0;
  88.     copyOfNum = inputNum;
  89.     while (copyOfNum != 0)
  90.     {
  91.         remainderOfNum = copyOfNum % 10;
  92.         reversedNum = reversedNum * 10 + remainderOfNum;
  93.         copyOfNum /= 10;
  94.     }
  95.     return reversedNum;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement