I need help fixing one problem with this functtion.
7 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
per isakson
2017-10-9
Try to put
if numRemoved == 0
num = 0;
newstr = str;
end
at the end of your function
更多回答(1 个)
Image Analyst
2017-10-9
编辑:Image Analyst
2017-10-9
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 个评论
Image Analyst
2017-10-9
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!