### Multiplying 2 strings (containing numbers) together

Hello all. Our tutor has asked us to multiplying two 2 digit numbers together, but they must first be placed into a string and it is the strings that must be multiplied together. The result is then to be displayed in reverse. I am very new to strings in C++ (as simple as they might be) and I cannot find out how to complete this task after nearly two hours of searching online now. Thank you in advance for any help given.

The bits have placed in italics cannot be removed (but can possibly be modified) as they are needed for this task or the next our tutor will give us.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include #include using namespace std; string intToStr(int num); string reverseString(string const s); int main() { int num1 = 86, num2 = 97, result; string strNum1 = intToStr(num1); string reverseStrNum1 = reverseString(strNum1); string strNum2 = intToStr(num2); string reverseStrNum2 = reverseString(strNum2); string strResult = intToStr(result); string reverseStrResult = reverseString(strResult); result = strNum1 *= strNum2; strResult = intToStr(result); reverseStrResult = reverseString(strResult); cout << strResult << endl; cout << reverseStrResult << endl; system("pause"); return 0; } string intToStr(int num) { //takes an int and returns a string stringstream ss; ss << num; return ss.str(); } //returns the reverse of the input string without altering it string reverseString(string const s) { string rev = string(s.rbegin(), s.rend()); return rev; }``````
Well, you can't simply convert back to integer, multiply, and convert to string yet again? Otherwise, there is no true "multiplication" of two string values unless you want to do some rather ugly stuff in binary. And if your teacher demands that you head in the binary route, you can simply look at ]http://en.wikipedia.org/wiki/Binary_multiplier to know how to multiply in binary. As you can see, it is a simple batch of shifts, and if-else statements. Converting back to decimal is as simple as a quick google search.
Last edited on
Topic archived. No new replies allowed.