Password

QUESTION

Danny has a possible list of passwords of Manny’s facebook account. All passwords length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be in the list.\n\nYou have to print the length of Manny’s password and it’s middle character.\n\nNote : The solution will be unique.\n\nINPUT \nThe first line of input contains the integer N, the number of possible passwords. \nEach of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.\n\nOUTPUT \nThe first and only line of output must contain the length of the correct password and its central letter.\n\nCONSTRAINTS \n 1N100.

ANSWER

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

class TestClass {
    public static void main(String args[] ) throws Exception {
  Scanner scan = new Scanner(System.in);
  int cases = scan.nextInt();
  
  String [] data = new String[cases];
  for(int x=0;x<cases;x++){
   data[x] = scan.next();
  }
  String mid="";
  for(int x=0;x<data.length-1;x++){
   for(int y=1;y<data.length;y++){
    if(palin(data[x]).equals(data[y]))
     mid = data[x].length()+" "+data[x].substring(data[x].length() / 2, data[x].length() / 2 + 1 );
     
   }
  }
  
  System.out.print(mid);
    }
    
    public static String palin(String x){
     String value = "";
     
     value = new StringBuffer(x).reverse().toString();
      
     return value;
    }
}

KMS Pico

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro