Accenture Coding Question | Next Letter | CodeWindow

Next Letter

The function accepts two characters ‘ch1’ and ‘ch2’ as the argument. ‘ch1’ and ‘ch2’ are alphabetical letters. Implement the function to find and return the next letter so that distance between ch1 and ch2. While counting distance if you exceed the letter ‘z’ then count the remaining distance starting from the letter ‘a’.

Distance between two letters in the number of letters between them.

Assumptions:
All input and output characters are lower case alphabets.

Example 1:
Input 1: c
Input 2: g

Output: k
Explanation:
The distance between the letter ‘c’ and ‘g’ is 3 (d,e,f). The next letter with distance 3 from letter ‘g’ is ‘k’. Thus the output is k.

Sample Input:
Input 1: r
Input 2: l

Sample Output:
f

Solution in Python 3:

# https://codewindow.in

s1=input()
s2=input()

asc_s1 = ord(s1)
asc_s2 = ord(s2)
#print(asc_s1)
#print(asc_s2)

count=asc_s2-asc_s1
print(chr(asc_s2+count))

# Join Telegram @codewindow

Output:

r
l
f
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *