How do i remove the letter a user inputs from a word?

3 ビュー (過去 30 日間)
Kevin Smith
Kevin Smith 2017 年 10 月 8 日
コメント済み: Guillaume 2017 年 10 月 8 日
I have a function [newstr, num]=removeLetter(str,letter) that is supposed to take a word and a letter selected by the user and remove the letter from the word, and displays the number of letters removed from the word. For example, if the input is:
>>[newstr, num]-removeLetter('Elephant','e') %the output would be
newstr= lphant
num= 2
I am pretty close to having the script right, but there are a couple of problems. Here is what I have:
function [newstr, num] = removeLetter(str, letter)
A=double(str);
B=double(letter);
numRemoved=0;
for i=2:length(A)
if double(str(i))==B
A(i-numRemoved)=[];
numRemoved=numRemoved+1;
num=numRemoved;
newstr=char(A);
end
end
If the word has none of the same letters as the 'letter' input it sends an error. For example, if you input [newstr,num]=removeLetter('Rice', 'T') it will give me an error where I want it to display
newstr= Rice
num=0
Oh and I cannot figure out how to get it to work for capital and lower case letters, my string only removes one or the other and I need it to be able to remove both.

回答 (4 件)

Cedric
Cedric 2017 年 10 月 8 日
編集済み: Cedric 2017 年 10 月 8 日
Simply:
function [newStr, num] = removeLetter( str, letter )
newStr = regexprep( str, letter, '', 'ignorecase' ) ;
num = length(str) - length(newStr) ;
end
Or
function [str, num] = removeLetter( str, letter )
n0 = length(str) ;
str(strfind(str, lower(letter))) = [] ;
str(strfind(str, upper(letter))) = [] ;
num = n0 - length(str) ;
end
Or
function [str, num] = removeLetter( str, letter )
n0 = length(str) ;
str(str == lower(letter) | str == upper(letter)) = [] ;
num = n0 - length(str) ;
end
Or (this is "Cody™ humor", don't use it ;))
function [newStr, ans] = removeLetter( str, letter )
newStr = regexprep( str, letter, '', 'ignorecase' ) ;
length(str) - length(newStr) ;
end

Guillaume
Guillaume 2017 年 10 月 8 日
All the conversions to double and back to char are completely unnecessary in your script (and in Salim answers). The exact same result would be obtained without them.
You cannot iterate forward over an array and remove elements. As soon as you remove an element your loop index is out of sync with the new indices of the array with deleted elements. You either need to store all the indices of the elements you want to remove in the loop and do the deletion in one go after the loop, or work backward from the end to the beginning

jean claude
jean claude 2017 年 10 月 8 日
function [newstr, num] = removeLetter(str, letter)
A=double(str);
B=double(letter);
numRemoved=0;
for i=2:length(A)
if double(str(i))==B
A(i-numRemoved)=[];
numRemoved=numRemoved+1;
num=numRemoved;
newstr=char(A);
else num=0 ;
newstr=char(A);
end
end
  1 件のコメント
Kevin Smith
Kevin Smith 2017 年 10 月 8 日
Hey Salim, I already tried this code. For some reason this code made 'num' always equal zero, even if there were some removed. And I still have not figured out how to test for capital and lower case letters at the same time.

サインインしてコメントする。


jean claude
jean claude 2017 年 10 月 8 日
i thisnk it works
function [newstr, num] = removeLetter(str, letter)
A=double(str);
B=double(letter);
C=upper(letter);
D=double(C);
E=lower(letter);
F=double(E);
num=0;
numRemoved=0;
for i=1:length(A)
if A(i)==B ||A(i)==D ||A(i)==F
A(i)=nan;
numRemoved=numRemoved+1;
num=numRemoved;
end
end
A(isnan(A)) = [] ;
newstr=char(A);
  1 件のコメント
Guillaume
Guillaume 2017 年 10 月 8 日
That's certainly a lot better than you initial answer. At least this works even if it's a bit awkward. A few comments:
  • There is absolutely no need to convert characters to double in order to compare them. The code would work exactly the same without all these conversions.
  • Avoid creating temporary variables if they're only going to be used once. Just have
if str(i) = letter || str(i) == upper(letter) || ...
  • The way to make a case insensitive comparison is to convert both side of the comparison to uppercase or lowercase rather than testing all possible combinations, hence:
if lower(str(i)) == lower(letter)
  • And if you're going to create temporary variables, the worst names is to just name then A, B, C, etc. Give them names that actually tell you what they contain such as letterupper, letterlower, etc.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by