% Given a string 's1' representing a non-negative integer s and a character 'c' % representing a digit, find s*c % Assume the output string is named s3 % % Algorithm: % convert 'c' to a number c % repeat the following, starting with the least significant digit: % convert the ith digit of s1 to a number x, find m = x*c+carry % convert the least significant digit of m to a character % and insert into the answer as the ith digit. Save the most % significant digit of m as the carry. If a carry exists at the % and, stick it on the front of s3. function s3 = multiplyLine(s1, c); if length(s1) < 1 s3 = '0'; % if s1 == [] answer = 0 else carry = 0; % define the carry s3 = ''; % define the answer % Change c to a number c = c-'0'; % From least significant digit, change digit chars to numbers, add with % carry to get new carry and new digit, convert new digit back to char % and stick into solution. If a number runs out of digits, use 0. for i=1:length(s1) m = (s1(length(s1)+1-i)-'0')*c + carry; carry = floor(m/10); % save the carry s3 = [char(mod(m,10)+'0') s3]; % compute the answer digit end % If there is a carry at the end, change to char and stick it at front if carry > 0 s3 = [char(carry+'0') s3]; end end