how do I count the upper and lowercase letters in a string

16 ビュー (過去 30 日間)
Mason  Krattli
Mason Krattli 2019 年 10 月 2 日
回答済み: Steven Lord 2022 年 10 月 20 日
I have been able to count the exact letter in a string but I’m having trouble counting the uppercase and lowercase letters for a particular letter in a string using a function. My professor has asked us not to use anything that will oversimplify the script.
  5 件のコメント
Mason  Krattli
Mason Krattli 2019 年 10 月 2 日
I undestand that, but instead of me explaining in a few paragraphs everything I have learned thus far, I thought I would see what other people would come up with. If it looked similar I would try to use but if not the input is still appreciated.
Guillaume
Guillaume 2019 年 10 月 2 日
Well, the easy way, and I'd argue the right and only way to do it properly in matlab would be to use isstrprop.
num_lower = nnz(isstrprop(yourstring, 'lower'));
num_upper = nnz(isstrprop(yourstring, 'upper'));
which will work properly across the whole unicode range unlike any code you'll be able to come up with which will most likely fail miserably on strings like 'ÑñƂƃ'.
But very likely, you're not allowed to use that. Problem is, we don't know what's in scope or not, so it's difficult to give you an answer.

サインインしてコメントする。

回答 (4 件)

Turlough Hughes
Turlough Hughes 2019 年 10 月 2 日
str='UlUlUlUl'
newStr = upper(str) % make a second string converting the original too all uppercase
% then use strcmp compare the two strings
for ii=1:length(str)
idx(ii)=strcmp(str(ii),newStr(ii));
end
num_uppr=nnz(idx)
num_lwr=length(str)-num_uppr
  1 件のコメント
Guillaume
Guillaume 2019 年 10 月 2 日
Well, that's certainly one way of overcomplicating things. Note that the loop is not needed, nor strcmp. The whole thing can be simplified to:
num_uppr = nnz(str == upper(str));
num_lwr = numel(str) - num_uppr;
Note that the code will not work for strings containing non-letters, eg. digits, spaces, punctuation, etc.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2019 年 10 月 2 日
if S(K) == 'A' || S(K) == 'B' || S(K) == 'C' | S(K) == 'D' etc
or
if S(K) >= 'A' && S(K) <= 'Z'
but your professor might consider that to be oversimplifying, perhaps.
Later, when you are not under those restrictions, you could consider using ismember() or isstrprop()

Jean Kassa Victoire
Jean Kassa Victoire 2022 年 10 月 20 日
str = "BEneDicT"
%Extract Letters
A = extractBefore(str,"ict")
%Count upper case letters
pat = characterListPattern('A','Z')
upper_count = count(str,pat)

Steven Lord
Steven Lord 2022 年 10 月 20 日
I'd probably just use isstrprop, but another possibility:
s = 'Abracadabra, Hocus Pocus!';
uppercaseLettersAndNonletters = s == upper(s)
uppercaseLettersAndNonletters = 1×25 logical array
1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 1
uppercaseLettersOnly = uppercaseLettersAndNonletters & isletter(s)
uppercaseLettersOnly = 1×25 logical array
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
ULANL = blanks(strlength(s));
ULANL(uppercaseLettersAndNonletters) = s(uppercaseLettersAndNonletters)
ULANL = 'A , H P !'
ULO = blanks(strlength(s));
ULO(uppercaseLettersOnly) = s(uppercaseLettersOnly)
ULO = 'A H P '
allThreeStrings = [s; ULANL; ULO]
allThreeStrings = 3×25 char array
'Abracadabra, Hocus Pocus!' 'A , H P !' 'A H P '

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by