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

조회 수: 4 (최근 30일)
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

답변 (3개)

Daniel M
Daniel M 2019년 10월 15일
편집: Daniel M 2019년 10월 15일
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

Rik
Rik 2019년 10월 15일
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.

Steven Lord
Steven Lord 2019년 10월 15일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by