Finding Missing Characters

String S which is a palindrome is passed as the input.
But just one alphabet A is missing in S. The program must print the missing alphabet A.
 Note: The FIRST alphabet of S will always be present.
Input Format: The first line contains S.
Output Format: The first line contains the missing alphabet A.
Boundary Conditions:
The length of the input string S is between 3 to 100. The FIRST alphabet of S will always be present.
Example Input/Output 1:
Input:
malayaam
Output:
l
Example Input/Output 2:
Input:
abcddcb
Output:
a
Code:
import java.util.*;
public class Hello {
    public static void main(String[] args) {
 Scanner sc=new Scanner(System.in);
 String str=sc.next();
 char[] ch=str.toCharArray();
 int i,j;
 for(i=0,j=ch.length-1;i<ch.length/2;i++,j–)
 {
     if(ch[i]!=ch[j])
     {
         if(ch[i]==ch[j-1]&&(i!=j-1))
            {
            System.out.print(ch[j]);
         break;
            }
            else
            {
              System.out.print(ch[i]);
              break;
            }
     }
 }
   
 }
}

Add a Comment

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