How do I create a function that takes a character array (s) as an input and returns a new character array with each letter shifted forward once in the alphabet?
18 次查看(过去 30 天)
显示 更早的评论
Assume that the input s will never contain the letter 'z'.
Example:
>> shiftletters('apple')
ans =
bqqmf
So far this is what I have and it must be because I'm an idiot but I can't get any further. Thank you in advance.
* function s = shiftletters('apple')
s=char('[A-Z]'+1);*
I believe my problem lies in the fact that I do not know how to correctly identify the letters of the alphabet into a single variable or array.
4 个评论
Yao Li
2013-4-9
Do you mean if the string is 12345abcdz23, the result should be 12345bcdea23? Currently, you function runs well except 'z' and the characters beyond alphabet. So before the shift, try to find out whether there are 'z' in the string and which charaters needs to be shifted.
irfan KHAN
2020-12-25
can anyone help me in the following
Create a variable named str which contains the characters 'wxyz'. Shift it forwards by three characters (i.e., add 3 to the ascii code). What is the result?
采纳的回答
更多回答(1 个)
Yao Li
2013-4-9
编辑:Yao Li
2013-4-9
function s=shiftletters(input_string)
for i=1:1:length(input_string)
if((double(input_string(i))>=65&&double(input_string(i))<=89)...
||(double(input_string(i))>=97&&double(input_string(i))<=121))
input_string(i)=char(input_string(i)+1);
elseif((double(input_string(i))==90)||(double(input_string(i))==122))
input_string(i)=char(input_string(i)-25);
end
end
s=input_string;
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!