I can't figure out what is wrong with the code. MATLAB

1 view (last 30 days)
function y = camelCase(x)
%camelCase Convert name with spaces to camelCase.
y(1) = lower(x(1));
% find the spaces
indall = find(x==' ');
% figure out where consecutive are
% and remove all
consec = diff(indall)==1;
ind = indall;
ind(consec) = [];
y = x;
y(min(ind+1,end)) = upper(y(min(ind+1,end)));
y(indall) = '';
end
This is what I am supposed to get
% out1 = camelCase('This Is a Variable')
% out1 => 'thisIsAVariable'
%
% out2 = camelCase('HERE IS AN EXAMPLE')
% out2 => 'hereIsAnExample'
%
% out3 = camelCase('the CoW jUmPeD over THE mooN')
% out3 => 'theCowJumpedOverTheMoon'
BUT I keep getting something else. ANY HELP!!!!!

Accepted Answer

Star Strider
Star Strider on 3 Feb 2015
There may be several ways to accomplish what you want.
Here is one:
S = 'HERE IS AN EXAMPLE';
sp = strfind(S, ' ');
uc = upper(S(sp+1));
SL = lower(S);
SL(sp+1) = uc;
Out = strrep(SL, ' ','')
produces:
Out =
hereIsAnExample

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!