Seperate numbers and Letters & Rearrange the String: Variable Letters Has an Incorrect Value

3 views (last 30 days)
Write the function Separator that:
Input: inString -- random string scalar mixed with numbers & letters
Tasks:
(1) Seperate Numbers & Letters
(2) Calculate Sum of Numbers
(3) Count Number of Letters (the white spaces counted)
Output:
(1) numbers, string scalar of all numbers in inString with same order.
(2) letters, string scalar of all letters in inString with same order, and white spaces Does Not removed.
(3) sumofNumbers, double precision scalar of sum of all numbers in inString.
(4) numberofLetters, double precision scalar to count all letters in inString, white spaces counted.
Expected Result:
inString: "eng12in13e143e553rin154g 6p547ro548bl645em 8s65ol9v56ing"
numbers: "12131435531546547548645865956"
letters: "engineering problem solving"
sumofNumbers: 131
numberofLetters: 27
Here's My Code:
function [numbers, letters, sumofNumbers, numberofLetters]=Separator(inString)
%Insert your code here
indexNum = regexp(char(inString), '[0-9]')
numbers = []
for i = 1:numel(indexNum)
numbers = [numbers , inString{1}(indexNum(i))]
end
% numbers
numbers = string(numbers)
% sumofNumbers
sumofNumbers = 0
for i = 1:numel(indexNum)
sumofNumbers = sumofNumbers + str2num(numbers{1}(i))
end
words = split(inString)
letters = []
count = 0
for i = 1:numel(words)
indexLett = regexp(char(words(i)), '[a-z]')
count = count + numel(indexLett)
for j = 1:numel(indexLett)
letters = [letters, words{i}(indexLett(j))]
end
letters = strcat(string(letters), " ")
letters = char(letters)
end
% letters
letters = strip(string(letters))
comb = split(letters)
letters = join(comb)
% number of Literal Letters
numofTrueLetters = count
% numberofLetters
numberofLetters = 0
numberofLetters = strlength(letters)
end
The Code Returns Exactly As the Expected:
numbers =
"12131435531546547548645865956"
letters =
"engineering problem solving"
sumofNumbers =
131
numberofLetters =
27
However, the MATLAB Grader gives this Answer: "Variable Letters Has an Incorrect Value" and I was confused.
I would be very appreciated if someone could point out the mistake or the error, thank you!
  5 Comments
Stephen23
Stephen23 on 13 Nov 2021
Edited: Stephen23 on 13 Nov 2021
"I think the white spaces between my letters might not be exact same as the expected value? "
That is certainly possible: if the test data includes other types of whitespace characters, then your code will not handle that correctly, e.g. tabs, non-breaking spaces, etc. The assignment specifically states "white spaces Does Not removed." but in your code you remove all of the whitespaces and replace them with space characters, i.e. char(32).
William Wang
William Wang on 13 Nov 2021
@Stephen Thank you for this reminding! I just look up the various types of whitespaces, and these could really conduct the different outputs. Understand now!

Sign in to comment.

Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!