MATLAB Answers

0

Find and count how many capital characters are in str, and then change them to lower cases USING LOOPS

Asked by taki shi on 15 Oct 2019
Latest activity Answered by Steven Lord
on 15 Oct 2019
how to do this with loops i know the regular way but i want to know with loops
please if you can help then do
but dont comment if you will just make random comments and not help
please
thank you

  0 Comments

Sign in to comment.

3 Answers

Answer by Daniel M
on 15 Oct 2019
Edited by Daniel M
on 15 Oct 2019

s = 'This is my String with CapiTal LETTERS in it. Let''s change them to lowercase uSiNg LoOpS.'
[caps,inds] = regexp(s,'[A-Z]','match');
numCaps = length(inds);
for n = 1:numCaps
s(inds(n)) = lower(caps{n});
end

  0 Comments

Sign in to comment.


Answer by Rik
on 15 Oct 2019

You can easily do this without loops:
s = 'This is my String with CapiTal LETTERS in it. Let''s change them to lowercase uSiNg LoOpS.'
s_out=lower(s);
N_upper_case=sum(s~=s_out);
If for some strange reason you insist on doing this with loops, you can easily add some indexing:
s = 'This is my String with CapiTal LETTERS in it. Let''s change them to lowercase uSiNg LoOpS.'
s_out=s;
N_upper_case=0;
for n=1:numel(s)
s_out(n)=lower(s_out(n));
N_upper_case=N_upper_case+double(s_out(n)~=s(n));
end
If you ask me the second option is much more complex (and slower) than the first, without getting any benefits.

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 15 Oct 2019

The isstrprop function can help you identify which characters are upper-case, which are lower-case, and which are neither. After that, your loop simply needs to see if the current letter is a different case than the next letter. Since I suspect this is a homework assignment I'm not going to give you the implementation; you should be able to complete it given these hints. If you can't, show us what you've written and ask a specific question about where you're having difficulty and we may be able to provide you with additional guidance.

  0 Comments

Sign in to comment.