Next value in alphabet

41 ビュー (過去 30 日間)
Riva
Riva 2023 年 1 月 18 日
編集済み: Walter Roberson 2023 年 1 月 19 日
I am trying to write a code to find a letter in the alphabet after the letter given, but I'm not sure which function specifies to choose the letter after. I've tried looking everywhere but I cant find it.
function [next] = nextLetter('char')
Alphabet = ('a';'b';'c';'d';'e';'f';'g';'h';'i';'j';'k';'l';'m';'n';'o';'p';'q';'r';'s';'t';'u';'v';'w';'x';'y';'z';)
next = find(ismember(Alphabet, ???)

回答 (4 件)

Les Beckham
Les Beckham 2023 年 1 月 18 日
A simpler approach:
char('c' + 1)
ans = 'd'

VBBV
VBBV 2023 年 1 月 18 日
編集済み: VBBV 2023 年 1 月 18 日
p = 'a' % e.g. user enters input letter
p = 'a'
nextLetter(p) % output of nextLetter
ans = 1×1 cell array
{'b'}
function [next] = nextLetter(p)
Alphabet = {'a';'b';'c';'d';'e';'f';'g';'h';'i';'j';'k';'l';'m';'n';'o';'p';'q';'r';'s';'t';'u';'v';'w';'x';'y';'z'};
next = find(ismember(Alphabet,p))+1; % add +1
next = Alphabet(next);
end
  1 件のコメント
VBBV
VBBV 2023 年 1 月 18 日
編集済み: VBBV 2023 年 1 月 18 日
Simply add +1 to the line , and pass the index to "Alphabet" array
next = find(ismember(Alphabet,p))+1;

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


Voss
Voss 2023 年 1 月 18 日
nextLetter('d')
ans = 'e'
nextLetter('s')
ans = 't'
nextLetter('z') % no next letter after 'z' -> error
Index exceeds the number of array elements. Index must not exceed 26.

Error in solution>nextLetter (line 8)
next = Alphabet(idx);
function next = nextLetter(letter)
Alphabet = 'a':'z';
idx = find(Alphabet == letter)+1;
next = Alphabet(idx);
end

Walter Roberson
Walter Roberson 2023 年 1 月 19 日
編集済み: Walter Roberson 2023 年 1 月 19 日
rot1('When is the time? The time for all good people, who come to the aid of their country!')
ans = 'Xifo jt uif ujnf? Uif ujnf gps bmm hppe qfpqmf, xip dpnf up uif bje pg uifjs dpvousz!'
function next = rot1(letter)
Next(1:255) = char(1:255);
Next('a':'y') = 'b':'z';
Next('A':'Y') = 'B':'Z';
Next('z') = 'a';
Next('Z') = 'A';
next = Next(letter);
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by