m2skills

smallest palindrome java

Apr 18th, 2017
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Scanner;
  3.  
  4. public class palindrome{
  5.    
  6.     public static void main(String arg[]){
  7.         boolean cont = true;
  8.         while(cont){
  9.             Scanner sc = new Scanner(System.in);
  10.             System.out.println("Enter the Number : ");
  11.             String inputString = sc.next();
  12.             nextSmallestPalindrome(inputString);
  13.            
  14.             System.out.println("Do you want to continue : ");
  15.             int cont1 = sc.nextInt();
  16.             if(cont1 == 0){
  17.                 cont = false;
  18.             }
  19.         }
  20.        
  21.     }
  22.    
  23.     public static void nextSmallestPalindrome(String inputString){
  24.        
  25.         String compString = "";
  26.         String finalString = "";
  27.         int length = inputString.length();
  28.         //case 1 checking if the String has a single digit less than 10
  29.         if(length == 1){
  30.             System.out.println("The next Smallest palindrome is  : 11");
  31.             return;
  32.         }
  33.        
  34.         for(int k=0; k<length; k++){
  35.             compString = compString + '9';
  36.         }
  37.        
  38.         //case 1 checking if all the digits in the number are 9
  39.         if(inputString.equals(compString)){
  40.             for(int k=0; k<length-1; k++){
  41.                 finalString = finalString + '0';
  42.             }
  43.             finalString = '1' + finalString + '1';
  44.             System.out.println("The next Smallest palindrome is  : " + finalString);
  45.             return;
  46.         }
  47.         //this block is entered when the number has digits other than 9
  48.         else{
  49.             boolean alreadyPalindrome = true;
  50.             int[] myNum = new int[length];
  51.             for(int k=0; k<length; k++){
  52.             char c = inputString.charAt(k);
  53.                 int temp = (int) (c-48);
  54.                 myNum[k] = temp;
  55.                 //System.out.println(myNum[k]);
  56.             }
  57.             int i=0,j=0;
  58.             // calculating mid positions
  59.             if(length%2 == 0){
  60.                 i = (length/2)-1;
  61.                 j=i+1;
  62.             }
  63.             else{
  64.                 i = (length/2)-1;
  65.                 j=i+2;
  66.             }
  67.            
  68.             //checking if the string is already Palindrome
  69.             int temp1 = i;
  70.             int temp2 = j;
  71.             while(temp1 > -1){
  72.                 if(myNum[temp1] != myNum[temp2]){
  73.                     alreadyPalindrome = false;
  74.                     break;
  75.                 }
  76.                 temp1 -= 1;
  77.                 temp2 += 1;
  78.             }
  79.            
  80.             //case when the entered String is a palindrome
  81.             if(alreadyPalindrome){
  82.                 if(length%2 == 0){
  83.                     System.out.println(alreadyPalindrome);
  84.                     while(myNum[i] == 9){
  85.                         myNum[i] = 0;
  86.                         myNum[j] = 0;
  87.                         i -= 1;
  88.                         j += 1;
  89.                     }
  90.                     myNum[i] += 1;
  91.                     myNum[j] += 1;
  92.                    
  93.                     //finalString = "".join(myNum);
  94.                     for(int c:myNum){
  95.                         char z = (char)(c);
  96.                         finalString += c;
  97.                     }
  98.                     System.out.println("The next Smallest palindrome is  : " + finalString);
  99.                    
  100.                 }
  101.                 //length of the entered String is not even
  102.                 else{
  103.                     if(myNum[i+1] == 9){
  104.                         myNum[i+1] = 0;
  105.                        
  106.                     }
  107.                     while(myNum[i] == 9){
  108.                         myNum[i] = 0;
  109.                         myNum[j] = 0;
  110.                         i -= 1;
  111.                         j += 1;
  112.                     }
  113.                     myNum[i] += 1;
  114.                     myNum[j] += 1;
  115.                    
  116.                     for(int c:myNum){
  117.                         char z = (char)(c);
  118.                         finalString += c;
  119.                     }
  120.                     System.out.println("The next Smallest palindrome is  : " + finalString);
  121.                 }
  122.             }
  123.             //this block is entered if the number is not already a palindrome
  124.             else{
  125.                 System.out.println("helo hi");
  126.                 //finding postion of non matching digits starting from the middle
  127.                 while(myNum[i] == myNum[j]){
  128.                     i -= 1;
  129.                     j += 1;
  130.                 }
  131.                
  132.                 //checking if the left side number is greater than right side number
  133.                 //if yes then copy left side digits to right side digits
  134.                 if(myNum[i] > myNum[j]){
  135.                    
  136.                     while(i > -1){
  137.                         myNum[j] = myNum[i];
  138.                         i -= 1;
  139.                         j += 1;
  140.                     }
  141.                     for(int c:myNum){
  142.                         char z = (char)(c);
  143.                         finalString += c;
  144.                     }
  145.                     System.out.println("The next Smallest palindrome is  : " + finalString);
  146.                 }
  147.                
  148.                 // if the number on the left is less than number in the right
  149.                 // increment all the elements between i to j
  150.                 // copy elements to left of i to elements to right of j including i,j
  151.                 else{
  152.                     //then increment all the numbers between i and j
  153.                     for(int k=i+1; k < j ; k++){
  154.                         myNum[k] +=1;
  155.                     }
  156.  
  157.                     if(i+1 == j){
  158.                         myNum[i] += 1;
  159.                         myNum[j] = myNum[i];
  160.                         i -= 1;
  161.                         j += 1;
  162.                     }
  163.  
  164.                     while(i > -1){
  165.                         myNum[j] = myNum[i];
  166.                         i -= 1;
  167.                         j += 1;
  168.                     }
  169.                    
  170.                     for(int c:myNum){
  171.                         char z = (char)(c);
  172.                         finalString += c;
  173.                     }
  174.                     System.out.println("The next Smallest palindrome is  : " + finalString);
  175.                 }
  176.             }
  177.         }
  178.     }
  179. }
Add Comment
Please, Sign In to add comment