2

I'm new to this site as well as to coding so the question might seem trivial but any help would be great (please don't just answer the problem if possible).

I'm trying to write a function where a string of letter is converted to the amount of lower case (loops and conditional are not allowed). My attempt so far is:

function countLowerCase
string = input('Please enter a string: ');
Lowercase = 'string' > 96 & 'string' <123;
sum(Lowercase)

Thanks in advance.

2
  • Are you allowed to use built-in functions like lower()? It would be even simpler if so. Commented Mar 9, 2014 at 18:50
  • 2
    Or can you use ISSTRPROP - mathworks.in/help/matlab/ref/isstrprop.html Commented Mar 9, 2014 at 18:52

2 Answers 2

3

Firstly, what's the difference between string and 'string'? One is a variable, the other is a constant string containing 5 lowercase characters. Now why does the function always return 5? Wait, it doesn't return anything because it has no output variable ;)

Mistakes aside, that's a perfectly valid approach assuming we're dealing with purely 7-bit ASCII characters so that "lowercase" implies a single contiguous range. One helpful tip to make the comparisons clearer is to use the characters themselves:

(spoilers)

Lowercase = (string >= 'a') & (string <= 'z');

More generally, since Matlab's handling of non-ASCII characters is locale-specific, in real code (rather than programming exercises) it would be inadvisable to do anything other than let the built-in language methods handle it:

sum(string == lower(string)); or sum(isstrprop(string, 'lower'));

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you use spoiler-style here? Are we on code-golf?
@mbschenkel Maybe I took "(please don't just answer the problem if possible)" too literally ;)
Thanks for the help and the quick response!
2

You almost have it:

function countLowerCase
string = input('Please enter a string: ','s'); %// add 's' to get a string
Lowercase = string > 96 & string <123; %// remove quotation marks
sum(Lowercase)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.