help solving string question
1 view (last 30 days)
Durring my last semester, we were given a bonus quiz to write a function the received and numberical input and produced the revise as an output.
we were not allowed to use any string variable or function. Any ideas now that the semester is over Id like to know what you think.
My original thought was to divide the input but the got harder with larger number as the function should receive any number
Star Strider on 7 May 2014
This works, but obviously only for integers:
a = 123;
% a = fix(rand*1E6) % Test Integer
La = fix(log10(a));
x = a;
for k1 = La:-1:0
d(k1+1) = fix(x/(10^k1));
x = rem(x,10^k1);
v10 = 10.^(La:-1:0)';
Flipped_a = d*v10
The Flipped_a variable is the result. I tested it on other random integers as well.
More Answers (4)
Carlos on 7 May 2014
a=[1 2 3 4 5];
Wesley Ooms on 7 May 2014
totally not optimized, but this will do the trick:
clear a b
Sagar Damle on 7 May 2014
Edited: Sagar Damle on 8 May 2014
I think the code which I am going to put here is the standard code to reverse a number.(This code is used in C language,of course,according to its own syntax!)Also,it is easy to understand.Remember this code,I think it is very helpful!
a = 126986;
b = a; % Save value of "a" in new variable "b".
reverse = 0;
while b > 0 % OR while b ~= 0 (Both 'while' statements are same.)
r = rem(b,10);
reverse = reverse * 10 + r;
b = floor(b/10);