m2skills

smallest palindrome c++

Apr 18th, 2017
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. #include<strings.h>
  4. #include<vector>
  5. #include<sstream>
  6. using namespace std;
  7. void nextSmallestPalindrome(string inputString)
  8. {
  9.     int length = inputString.length();
  10.     string compString = "";
  11.     string finalString = "";
  12.     stringstream ss;
  13.     int j=0,i=0;
  14.     //if the number is less than 9 then the smallest palindrome is 11
  15.     if(length == 1 && inputString < "9") {
  16.         cout<<"The next smallest palindrome is : 11";
  17.     }
  18.     for (int k=0; k<length;k++){
  19.         compString += "9";
  20.     }
  21.  
  22.     //case 1 if the input string contains all 9's
  23.     if(inputString == compString) {
  24.         for (int k=0; k<length-1;k++) {
  25.             finalString += "0";
  26.         }
  27.         finalString = "1" + finalString + "1";
  28.         cout<<"The next smallest palindrome is : "<<finalString;
  29.     }
  30.         //if the input string contains digits other than 9
  31.     else {
  32.         //convert string to vector to make it easy for manipulation
  33.         vector<int> myStr;
  34.         for(unsigned int k=0; k<inputString.length(); k++) {
  35.             myStr.push_back((int) (inputString[k]-48));
  36.         }
  37.  
  38.         //calculating mid positions
  39.         if (length%2 == 0) {
  40.             i = int(length/2) - 1;
  41.             j = i+1;
  42.         }
  43.         else {
  44.             i = int(length/2) - 1;
  45.             j = i+2;
  46.         }
  47.  
  48.         //checking if the entered string is already a palindrome
  49.         bool alreadyPalindrome = true;
  50.         int t1 = i,t2 = j;
  51.         while(t1>-1) {
  52.             if(myStr[t1] != myStr[t2]) {
  53.                 alreadyPalindrome = false;
  54.                 break;
  55.             }
  56.             t1 -=1;
  57.             t2 +=1;
  58.         }
  59.  
  60.         if(alreadyPalindrome){
  61.             if(length%2 == 0){
  62.                 if(myStr[i]!=9 && myStr[j]!= 9){
  63.                     myStr[i] += 1;
  64.                     myStr[j] +=1;
  65.                 }
  66.                 else{
  67.                     while(myStr[i] == 9){
  68.                         myStr[i] = 0;
  69.                         myStr[j] = 0;
  70.                         i -= 1;
  71.                         j += 1;
  72.                     }
  73.                     myStr[i] += 1;
  74.                     myStr[j] += 1;
  75.                 }
  76.                 for(unsigned int k=0; k<myStr.size(); k++)
  77.                 {
  78.                     ss << myStr[k];
  79.                 }
  80.                 finalString = ss.str();
  81.                 //ss.clear();
  82.                 cout<<"The next smallest palindrome is : "<<finalString;
  83.             }
  84.             else{
  85.                 int t1 = length/2;
  86.                 if(myStr[t1] == 9){
  87.                     myStr[t1] = 0;
  88.                     while(myStr[i] == 9){
  89.                         myStr[i] = 0;
  90.                         myStr[j] = 0;
  91.                         i -= 1;
  92.                         j += 1;
  93.                     }
  94.                     myStr[i] += 1;
  95.                     myStr[j] += 1;
  96.                 }
  97.                 else{
  98.                     myStr[t1] += 1;
  99.                 }
  100.                 for(unsigned int k=0; k<myStr.size(); k++)
  101.                 {
  102.                     ss << myStr[k];
  103.                 }
  104.                 finalString = ss.str();
  105.                 //ss.clear();
  106.                 cout<<"The next smallest palindrome is : "<<finalString;
  107.             }
  108.         }
  109.         // if the string is not already a palindrome
  110.         else {
  111.             //starting from middle leave all the digits that are same on both sides
  112.             while(myStr[i] == myStr[j]){
  113.                 i -= 1;
  114.                 j += 1;
  115.             }
  116.             //if the number on the left side is greater than the number on the right side
  117.             // then copy the left side to right side
  118.             if(myStr[i] > myStr[j]){
  119.                 while(i > -1){
  120.                     myStr[j] = myStr[i];
  121.                     i -= 1;
  122.                     j += 1;
  123.                 }
  124.                 for(int k=0; k < myStr.size(); k++){
  125.                     ss<<myStr[k];
  126.                 }
  127.                 finalString = ss.str();
  128.                 cout<<"The next smallest palindrome is : "<<finalString;
  129.             }
  130.             // if the number on the left is less than the number on the right
  131.             // then increment all the numbers between i and j
  132.             // then if i+1 == j then increment ith element  and copy it to jth element
  133.             // then copy the remaining left side to right side
  134.             else{
  135.                 //then increment all the numbers between i and j
  136.                 for(int k=i+1; k < j ; k++){
  137.                     myStr[k] +=1;
  138.                 }
  139.  
  140.                 if(i+1 == j){
  141.                     myStr[i] += 1;
  142.                     myStr[j] = myStr[i];
  143.                     i -= 1;
  144.                     j += 1;
  145.                 }
  146.  
  147.                 while(i > -1){
  148.                     myStr[j] = myStr[i];
  149.                     i -= 1;
  150.                     j += 1;
  151.                 }
  152.                 for(int k=0; k < myStr.size(); k++){
  153.                     ss<<myStr[k];
  154.                 }
  155.                 finalString = ss.str();
  156.                 cout<<"\nThe next smallest palindrome is : "<<finalString;
  157.             }
  158.         }
  159.     }
  160. }
  161. int main()
  162. {
  163.     string inputString;
  164.     int temp;
  165.     bool cont = true;
  166.     while(cont){
  167.         cout << "Enter the number : ";
  168.         cin>>inputString;
  169.         nextSmallestPalindrome(inputString);
  170.         cout<<"\nDo you want to continue (1/0) : "<<endl;
  171.         cin>>temp;
  172.         if(temp == 0){
  173.             cont = false;
  174.         }
  175.     }
  176.  
  177.     return 0;
  178. }
Add Comment
Please, Sign In to add comment