C# || How To Multiply Two Strings Using C#
The following is a module with functions which demonstrates how to multiply two strings together using C#.
1. Multiply Strings – Problem Statement
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
2. Multiply Strings – Solution
The following is a solution which demonstrates how to multiply two strings together.
This solution starts from the end of both strings and multiplies each individual number, keeping track of any carryovers. The result of each operation is stored in a ‘solution’ array, and when the operation is complete, the result is returned as a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 13, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to multiply two strings together // ============================================================================ public class Solution { public string Multiply(string num1, string num2) { if (num1 == "0" || num2 == "0") { return "0"; } // This will be the max number of digits var solution = new int[num1.Length + num2.Length]; // Loop through both strings from the end for (int num1Index = num1.Length -1; num1Index >= 0; --num1Index) { for (int num2Index = num2.Length -1; num2Index >= 0; --num2Index) { // Get the current solution index var currentIndex = num1Index + num2Index + 1; // Multiply the two numbers var value = CharToInt(num1[num1Index]) * CharToInt(num2[num2Index]); // Add the value to the result at the current index solution[currentIndex] += value; // Check to see if there is any carry to be removed if (solution[currentIndex] > 9) { // Add the carry to the 'previous' index var carry = solution[currentIndex] / 10; solution[currentIndex - 1] += carry; // Remove the carry from the current index var digit = solution[currentIndex] % 10; solution[currentIndex] = digit; } } } // Skip over leading zeroes var zeroIndex = 0; while (zeroIndex < solution.Length && solution[zeroIndex] == 0) { ++zeroIndex; } // Convert to string and return var result = new StringBuilder(); for (int index = zeroIndex; index < solution.Length; ++index) { result.Append(solution[index]); } return result.ToString(); } public int CharToInt(char ch) { return ch - '0'; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
"6"
"56088"
Leave a Reply