How do i remove the letter a user inputs from a word?
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (4)
Cedric
on 8 Oct 2017
Edited: Cedric
on 8 Oct 2017
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
0 Comments
Guillaume
on 8 Oct 2017
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
0 Comments
jean claude
on 8 Oct 2017
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
jean claude
on 8 Oct 2017
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 Comment
Guillaume
on 8 Oct 2017
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.
See Also
Categories
Find more on Logical 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!