multiplying strings code example

Example: multiply strings

string Solution::multiply(string A, string B) 
{    
  vector<int> arr(A.size()+B.size(),0);    
  for(int i=A.size()-1;i>=0;i--)    
  {        
    for(int j =B.size()-1;j>=0;j--)        
    {            
      int x = (B[j]-'0')*(A[i]-'0')+arr[i+j+1];           
      arr[i+j+1] = x%10;            
      arr[i+j] += x/10;        
    }    
  }    
  int i=0;    
  while(i<arr.size())    
  {        
    if(arr[i]!=0)            
      break;            
    i++;    
  }                
  if(i == arr.size())        
    return "0";                
  string s = "";        
  for(int j=i;j<arr.size();j++)        
    s += to_string(arr[j]);            
  return s;    
}

Tags:

Cpp Example