Find the Mid-Alphabet for each index of the given Pair of Strings

Given two same length strings str1 and str2 consisting of lowercase English alphabets, the task is to find the Mid-Alphabet for each index of the given pair of Strings.

Examples:

Input: str1 = “abcd”, str2 = “cdef”
Output: bcde
Explanation:
b is mid of a and c
c is mid of b and d
d is mid of c and e
e is mid of e and f



Input: str1 = “akzbqzgw”, str2 = “efhctcsz”
Output: chqbrnmx

Approach:
The Mid-Alphabet can be calculated by taking an average of the ASCII values of the characters in each string at that index.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the Mid-Alphabet
// for each index of the given Pair of Strings
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the mid alphabets
void findMidAlphabet(string s1, string s2, int n)
{
  
    // For every character pair
    for (int i = 0; i < n; i++) {
  
        // Get the average of the characters
        int mid = (s1[i] + s2[i]) / 2;
        cout << (char)mid;
    }
}
  
// Driver code
int main()
{
  
    string s1 = "akzbqzgw";
    string s2 = "efhctcsz";
    int n = s1.length();
  
    findMidAlphabet(s1, s2, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the Mid-Alphabet
// for each index of the given Pair of Strings
class GFG
{
  
// Function to find the mid alphabets
static void findMidAlphabet(String s1,
                            String s2, int n)
{
  
    // For every character pair
    for (int i = 0; i < n; i++)
    {
  
        // Get the average of the characters
        int mid = (s1.charAt(i) + 
                   s2.charAt(i)) / 2;
        System.out.print((char)mid);
    }
}
  
// Driver code
public static void main(String []args)
{
    String s1 = "akzbqzgw";
    String s2 = "efhctcsz";
    int n = s1.length();
  
    findMidAlphabet(s1, s2, n);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the Mid-Alphabet
# for each index of the given Pair of Strings
  
# Function to find the mid alphabets
def findMidAlphabet(s1, s2, n):
  
    # For every character pair
    for i in range(n):
  
        # Get the average of the characters
        mid = (ord(s1[i]) + ord(s2[i])) // 2
        print(chr(mid), end = "")
  
# Driver code
s1 = "akzbqzgw"
s2 = "efhctcsz"
n = len(s1)
  
findMidAlphabet(s1, s2, n)
  
# This code is contributed
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the Mid-Alphabet
// for each index of the given Pair of Strings
using System;
      
public class GFG
{
   
// Function to find the mid alphabets
static void findMidAlphabet(String s1,
                            String s2, int n)
{
   
    // For every character pair
    for (int i = 0; i < n; i++)
    {
   
        // Get the average of the characters
        int mid = (s1[i] + 
                   s2[i]) / 2;
        Console.Write((char)mid);
    }
}
   
// Driver code
public static void Main(String []args)
{
    String s1 = "akzbqzgw";
    String s2 = "efhctcsz";
    int n = s1.Length;
   
    findMidAlphabet(s1, s2, n);
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

chqbrnmx

Time Complexity: O(N), where N is the length of the String.



My Personal Notes arrow_drop_up

Talk is cheap Show me the code -)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.