Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Scanner;
- public class palindrome{
- public static void main(String arg[]){
- boolean cont = true;
- while(cont){
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the Number : ");
- String inputString = sc.next();
- nextSmallestPalindrome(inputString);
- System.out.println("Do you want to continue : ");
- int cont1 = sc.nextInt();
- if(cont1 == 0){
- cont = false;
- }
- }
- }
- public static void nextSmallestPalindrome(String inputString){
- String compString = "";
- String finalString = "";
- int length = inputString.length();
- //case 1 checking if the String has a single digit less than 10
- if(length == 1){
- System.out.println("The next Smallest palindrome is : 11");
- return;
- }
- for(int k=0; k<length; k++){
- compString = compString + '9';
- }
- //case 1 checking if all the digits in the number are 9
- if(inputString.equals(compString)){
- for(int k=0; k<length-1; k++){
- finalString = finalString + '0';
- }
- finalString = '1' + finalString + '1';
- System.out.println("The next Smallest palindrome is : " + finalString);
- return;
- }
- //this block is entered when the number has digits other than 9
- else{
- boolean alreadyPalindrome = true;
- int[] myNum = new int[length];
- for(int k=0; k<length; k++){
- char c = inputString.charAt(k);
- int temp = (int) (c-48);
- myNum[k] = temp;
- //System.out.println(myNum[k]);
- }
- int i=0,j=0;
- // calculating mid positions
- if(length%2 == 0){
- i = (length/2)-1;
- j=i+1;
- }
- else{
- i = (length/2)-1;
- j=i+2;
- }
- //checking if the string is already Palindrome
- int temp1 = i;
- int temp2 = j;
- while(temp1 > -1){
- if(myNum[temp1] != myNum[temp2]){
- alreadyPalindrome = false;
- break;
- }
- temp1 -= 1;
- temp2 += 1;
- }
- //case when the entered String is a palindrome
- if(alreadyPalindrome){
- if(length%2 == 0){
- System.out.println(alreadyPalindrome);
- while(myNum[i] == 9){
- myNum[i] = 0;
- myNum[j] = 0;
- i -= 1;
- j += 1;
- }
- myNum[i] += 1;
- myNum[j] += 1;
- //finalString = "".join(myNum);
- for(int c:myNum){
- char z = (char)(c);
- finalString += c;
- }
- System.out.println("The next Smallest palindrome is : " + finalString);
- }
- //length of the entered String is not even
- else{
- if(myNum[i+1] == 9){
- myNum[i+1] = 0;
- }
- while(myNum[i] == 9){
- myNum[i] = 0;
- myNum[j] = 0;
- i -= 1;
- j += 1;
- }
- myNum[i] += 1;
- myNum[j] += 1;
- for(int c:myNum){
- char z = (char)(c);
- finalString += c;
- }
- System.out.println("The next Smallest palindrome is : " + finalString);
- }
- }
- //this block is entered if the number is not already a palindrome
- else{
- System.out.println("helo hi");
- //finding postion of non matching digits starting from the middle
- while(myNum[i] == myNum[j]){
- i -= 1;
- j += 1;
- }
- //checking if the left side number is greater than right side number
- //if yes then copy left side digits to right side digits
- if(myNum[i] > myNum[j]){
- while(i > -1){
- myNum[j] = myNum[i];
- i -= 1;
- j += 1;
- }
- for(int c:myNum){
- char z = (char)(c);
- finalString += c;
- }
- System.out.println("The next Smallest palindrome is : " + finalString);
- }
- // if the number on the left is less than number in the right
- // increment all the elements between i to j
- // copy elements to left of i to elements to right of j including i,j
- else{
- //then increment all the numbers between i and j
- for(int k=i+1; k < j ; k++){
- myNum[k] +=1;
- }
- if(i+1 == j){
- myNum[i] += 1;
- myNum[j] = myNum[i];
- i -= 1;
- j += 1;
- }
- while(i > -1){
- myNum[j] = myNum[i];
- i -= 1;
- j += 1;
- }
- for(int c:myNum){
- char z = (char)(c);
- finalString += c;
- }
- System.out.println("The next Smallest palindrome is : " + finalString);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment