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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by