Guest User

Untitled

a guest
Dec 31st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. // C program to multiply two numbers represented
  2. // as strings.
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. // Multiplies str1 and str2, and prints result.
  8. char* multiply(char* num1, char* num2)
  9. {
  10.     int n1 = strlen(num1);
  11.     int n2 = strlen(num2);
  12.  
  13.     if (n1 == 0 || n2 == 0)
  14.         return "0";
  15.  
  16.     // will keep the result number in vector
  17.     // in reverse order
  18.     int* result = malloc((n1+n2) * sizeof(int));
  19.     for (int k = 0; k < (n1+n2); ++k){
  20.         result[k] = 0;
  21.     }
  22.  
  23.     // Below two indexes are used to find positions
  24.     // in result.
  25.     int i_n1 = 0;
  26.     int i_n2 = 0;
  27.  
  28.     // Go from right to left in num1
  29.     for (int i=n1-1; i>=0; i--)
  30.     {
  31.         int carry = 0;
  32.         int n1 = num1[i] - '0';
  33.  
  34.         // To shift position to left after every
  35.         // multiplication of a digit in num2
  36.         i_n2 = 0;
  37.  
  38.         // Go from right to left in num2
  39.         for (int j=n2-1; j>=0; j--)
  40.         {
  41.             // Take current digit of second number
  42.             int n2 = num2[j] - '0';
  43.  
  44.             // Multiply with current digit of first number
  45.             // and add result to previously stored result
  46.             // at current position.
  47.             int sum = n1*n2 + result[i_n1 + i_n2] + carry;
  48.  
  49.             // Carry for next iteration
  50.             carry = sum/10;
  51.  
  52.             // Store result
  53.             result[i_n1 + i_n2] = sum % 10;
  54.  
  55.             i_n2++;
  56.         }
  57.  
  58.         // store carry in next cell
  59.         if (carry > 0)
  60.             result[i_n1 + i_n2] += carry;
  61.  
  62.         // To shift position to left after every
  63.         // multiplication of a digit in num1.
  64.         i_n1++;
  65.     }
  66.  
  67.     // ignore '0's from the right
  68.     int i = (n1+n2) - 1;
  69.     while (i>=0 && result[i] == 0)
  70.         i--;
  71.  
  72.     // If all were '0's - means either both or
  73.     // one of num1 or num2 were '0'
  74.     if (i == -1)
  75.         return "0";
  76.  
  77.     // generate the result string
  78.     char* s;
  79.     s = malloc(i*sizeof(char));
  80.     s[0] = '\0';
  81.  
  82.     char *buffer = malloc(sizeof(char));
  83.     buffer[0] = '\0';
  84.     while (i >= 0){
  85.         sprintf(buffer,"%d",result[i--]);
  86.         strcat(s,buffer);
  87.     };
  88.  
  89.     free(buffer);
  90.     free(result);
  91.  
  92.     return s;
  93. }
  94.  
  95. // Driver code
  96. int main()
  97. {
  98.     char* str1 = "12";
  99.     char* str2 = "12";
  100.  
  101.     printf("%s\n",multiply(str1, str2));
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment