Join Regular Classroom : Visit ClassroomTech

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

Alternative :

s1=input()
s2=input()

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

count=asc_s2-asc_s1
if chr(asc_s2+count) <= 'z':
	print(chr(asc_s2+count))
else:
	print(chr(ord('a') + asc_s2+count - ord('z')-1))

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>

int main()
{
    char a, b, c;
    scanf("%c",&a);
    scanf("%c",&c); // to avoid enter
    scanf("%c",&b);
    int count = b-a;
    if(b+count > 'z')
        printf("%c",'a'+b+count-'z'-1);
    else
        printf("%c",b+count);
    return 0;
}

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
int main()
{
    char a, b, c;
    cin >> a;
    cin >> b;
    int count = b-a;
    if(b+count > 'z')
        cout <<(char)('a'+b+count-'z'-1);
    else
        cout << (char)(b+count);
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.io.*;
import java.lang.*;
import java.util.*;

class CodeWindow {
    public static void main(String[] args) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        char a = sc.next().charAt(0);
        char b = sc.next().charAt(0);
        int count = b-a;
        if(b+count > 'z')
            System.out.println((char)('a'+b+count-'z'-1));
        else
            System.out.println((char)(b+count));
    }
}

Output:

r
l
f
Recent Posts
Pages