I need help fixing one problem with this functtion.

1 view (last 30 days)
I wrote this function 'removeLetter' to take any selected letter out of a given string and also displays the number of letters that were removed, for example, if a user inputs
[newstr, num]=removeLetter('water bottle', 't')
the output will be:
newstr= waer bole
num=3
the code does the function I need it to do, and works for lower and upper case letters. This is the script:
function [newstr, num] = removeLetter(str, letter)
A=double(str); %A represents the string as a number
B=double(letter); %B represents the letter as a number
numRemoved=0;
for i=2:length(A)
if double(str(i))==B || double(str(i))-32==B || double(str(i))+32==B %checks for capital and lower case letters depending on what the input letter and string was.
A(i-numRemoved)=[];
numRemoved=numRemoved+1;
num=numRemoved;
newstr=char(A);
end
end
I realize there are better ways to write this script using other built in function but I cannot use any functions we have not learned in class yet. We have learned a fair amount of functions but some individuals on here recommended some functions I cannot use yet, such as 'regexprep' and 'strfind.' This is why I am using the 'double' and 'char' functions, this is what we are working with in class so this is what I am expected to use. The only trouble I am having completing my script is the error I get when the letter entered is not in the string. For example I would need it to look like this:
[newstr, num]=removeLetter('pizza', 'r')
newstr= pizza
num=0
but instead of the desired output, it gives me an error saying "Output argument 'newstr' (and maybe others) not assigned during call to 'removeLetter'".
I tried using things like 'if str~=B (the variable for the letter) then num=0 and newstr=str' but that just made the number equal zero every time even if the string contained the letter. I just need to know how to fix it so it displays '0' instead of this error when the letter isn't in the string.

Accepted Answer

per isakson
per isakson on 9 Oct 2017
Try to put
if numRemoved == 0
num = 0;
newstr = str;
end
at the end of your function
  5 Comments
Kevin Smith
Kevin Smith on 9 Oct 2017
Actually it did not work, I thought it did but when I entered a string with the letter it displayed the same string without the letter missing and the number always equals 0 now.
Kevin Smith
Kevin Smith on 9 Oct 2017
Never mind I had it in the wrong place. it did work.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Oct 2017
Edited: Image Analyst on 9 Oct 2017
This is how I'd do it:
function [newstr, num] = removeLetter(str, letter)
indexes = str == letter;
num = sum(indexes);
newstr = strrep(str, letter, '');
You can also do
newstr = str;
newstr(indexes) = [];
instead of the strrep() line.
  2 Comments
Kevin Smith
Kevin Smith on 9 Oct 2017
Hello there, I appreciate your answer, but we have not discussed the 'indexes' function, so my professor would not allow me to use it.
Image Analyst
Image Analyst on 9 Oct 2017
Kevin, that's not a function. It's a variable. You're allowed to call a variable "indexes". It's a lot better and more descriptive than x or a like people usually call variables. It's a logical index that say whether or not the letter is in each position of the string. Just take off the semicolons and see what it prints out.
My solution was much better than yours or per's solution. It's just ridiculous to convert your string to a double when strrep() is a built-in function to do what you want. If your professor also disallows strrep() for some weird reason, then simply use my last chunk of code to replace it.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!