In: Computer Science
MATLAB; How to find LowerCaseCount, UpperCaseCount, NumericCount, OtherCount.
Using the input file of characters, determine
% the number of characters that meet the following criterion. Each output
% variable will be a 1 x 1 scalar (i.e. how many of each there are).
% Input: 'Matlab_Wikipedia.mat' (variable name is Contents)
% Output: LowerCaseCount, UpperCaseCount, NumericCount, OtherCount
MATLAB Code:
function [LowerCaseCount, UpperCaseCount, NumericCount,
OtherCount] = characterCriteria (contents)
% Function that counts the number of characters
that matches each criteria
% Initializing output variables to 0
LowerCaseCount = 0;
UpperCaseCount = 0;
NumericCount = 0;
OtherCount = 0;
% Opening file in read mode
fp = fopen(contents, "r");
% Reading data from file
data = fscanf(fp, "%s");
% Iterating over char by char
for i = 1:length(data)
% Checking for
numbers
if isdigit(data(i))
% Updating numbers count
NumericCount += 1;
% Checking for lower
case letters
elseif
islower(data(i))
% Updating lower case count
LowerCaseCount += 1;
% Checking for upper
case letters
elseif
isupper(data(i))
% Updating upper case count
UpperCaseCount += 1;
% Other
characters
else
% Updating other count
OtherCount += 1;
endif
endfor
% Closing file
fclose(fp);
endfunction
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: