0

I need to write a function string add(string a, string b) where a and b are strings representing integers and the function add(a,b) returns a string representing their sum. Strings a and b can have a maximum of 100 characters.

I have tried different ways but failed, here is where I'm standing right now. So I took the 2 strings and I tried adding each digit starting from last. If in the array at [i] it's more than 10, then add 1 to [i-1], and mod it by 10 to get the last digit.

The return is empty:

string add(string a, string b){


 int arrA[a.length()];
    int arrB[b.length()];
    string Res=" ";


//99999999 2222222
if(a.length()>=b.length()){
    //i=7
    for (int i=b.length();i>=0;i--){
    arrA[i] = (int) (a[i]-'0') + (int) (b[i]-'0');
    }

         for(int i=b.length()-1;i>=1;i--)    
         Res[i]=arrA[i];

    for(int i=a.length()-1;i>=1;i--){

        if (arrA[i]>=10){
    arrA[i]=arrA[i]%10;
    arrA[i-1]=arrA[i-1]+1;}
    }
}
else{

    for (int i=a.length();i>=0;i--){
    arrB[i] = (int) (a[i]-'0') + (int) (b[i]-'0');
   }

         for(int i=b.length()-1;i>=1;i--)
         Res[i]=arrB[i];

    for(int i=b.length()-1;i>=1;i--){
        if (arrB[i]>=10){
    arrB[i]=arrB[i]%10;
    arrB[i-1]=arrB[i-1]+1;}
    }

}
  return Res;

}

Thank you in advance!

6
  • 1
    Why not convert both strings to integers, add them, convert the result of the addition back to a string? Commented Feb 2, 2020 at 17:16
  • @JesperJuhl I did not know you can do that tbh. This is CS 111, very basic. Commented Feb 2, 2020 at 17:23
  • Some pointers: en.cppreference.com/w/cpp/string/basic_string/stol , en.cppreference.com/w/cpp/string/basic_string/to_string - no need for all those loops.. Commented Feb 2, 2020 at 17:26
  • May have some problems with the 100 digit strings, though. Commented Feb 2, 2020 at 17:35
  • @Eljay True. That's where bignum libraries like GMP (and friends) enter the picture.. Commented Feb 2, 2020 at 17:37

2 Answers 2

1

Think about how you would do this with pencil and paper, then write code to do the same thing.

You have two strings of digits. Start at the right, add the two digits, and if the result overflows, subtract 10 and note that you have a carry. Store the resulting digit. Move one place to the left. Repeat until done. If you run out of digits in one string, just pretend that you've got zeros for the rest of the digits.

Note that each digit in the input is the character representation of the digit. To get the numeric value, subtract '0' from each digit. Once you have the result, convert it to a character by adding '0'.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what I tried to do, but my code isn't working. I started at the right, added, and then modded by 10 to get the digit for arr[i]. Then if it's more than 10 then add a +1 to arr[i-1].
@Dave -- think it through carefully. You only need one loop, not the half-dozen that the code in the question has. Again: picture how you would do it with pencil and paper.
0
string add(string a, string b) {
    int c = stoi(a) + stoi(b);
    return to_string(c);
}

1 Comment

You've missed the requirement that the strings can have up to 100 characters. Your solution won't work because of integer overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.