Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<string>
- #include<strings.h>
- #include<vector>
- #include<sstream>
- using namespace std;
- void nextSmallestPalindrome(string inputString)
- {
- int length = inputString.length();
- string compString = "";
- string finalString = "";
- stringstream ss;
- int j=0,i=0;
- //if the number is less than 9 then the smallest palindrome is 11
- if(length == 1 && inputString < "9") {
- cout<<"The next smallest palindrome is : 11";
- }
- for (int k=0; k<length;k++){
- compString += "9";
- }
- //case 1 if the input string contains all 9's
- if(inputString == compString) {
- for (int k=0; k<length-1;k++) {
- finalString += "0";
- }
- finalString = "1" + finalString + "1";
- cout<<"The next smallest palindrome is : "<<finalString;
- }
- //if the input string contains digits other than 9
- else {
- //convert string to vector to make it easy for manipulation
- vector<int> myStr;
- for(unsigned int k=0; k<inputString.length(); k++) {
- myStr.push_back((int) (inputString[k]-48));
- }
- //calculating mid positions
- if (length%2 == 0) {
- i = int(length/2) - 1;
- j = i+1;
- }
- else {
- i = int(length/2) - 1;
- j = i+2;
- }
- //checking if the entered string is already a palindrome
- bool alreadyPalindrome = true;
- int t1 = i,t2 = j;
- while(t1>-1) {
- if(myStr[t1] != myStr[t2]) {
- alreadyPalindrome = false;
- break;
- }
- t1 -=1;
- t2 +=1;
- }
- if(alreadyPalindrome){
- if(length%2 == 0){
- if(myStr[i]!=9 && myStr[j]!= 9){
- myStr[i] += 1;
- myStr[j] +=1;
- }
- else{
- while(myStr[i] == 9){
- myStr[i] = 0;
- myStr[j] = 0;
- i -= 1;
- j += 1;
- }
- myStr[i] += 1;
- myStr[j] += 1;
- }
- for(unsigned int k=0; k<myStr.size(); k++)
- {
- ss << myStr[k];
- }
- finalString = ss.str();
- //ss.clear();
- cout<<"The next smallest palindrome is : "<<finalString;
- }
- else{
- int t1 = length/2;
- if(myStr[t1] == 9){
- myStr[t1] = 0;
- while(myStr[i] == 9){
- myStr[i] = 0;
- myStr[j] = 0;
- i -= 1;
- j += 1;
- }
- myStr[i] += 1;
- myStr[j] += 1;
- }
- else{
- myStr[t1] += 1;
- }
- for(unsigned int k=0; k<myStr.size(); k++)
- {
- ss << myStr[k];
- }
- finalString = ss.str();
- //ss.clear();
- cout<<"The next smallest palindrome is : "<<finalString;
- }
- }
- // if the string is not already a palindrome
- else {
- //starting from middle leave all the digits that are same on both sides
- while(myStr[i] == myStr[j]){
- i -= 1;
- j += 1;
- }
- //if the number on the left side is greater than the number on the right side
- // then copy the left side to right side
- if(myStr[i] > myStr[j]){
- while(i > -1){
- myStr[j] = myStr[i];
- i -= 1;
- j += 1;
- }
- for(int k=0; k < myStr.size(); k++){
- ss<<myStr[k];
- }
- finalString = ss.str();
- cout<<"The next smallest palindrome is : "<<finalString;
- }
- // if the number on the left is less than the number on the right
- // then increment all the numbers between i and j
- // then if i+1 == j then increment ith element and copy it to jth element
- // then copy the remaining left side to right side
- else{
- //then increment all the numbers between i and j
- for(int k=i+1; k < j ; k++){
- myStr[k] +=1;
- }
- if(i+1 == j){
- myStr[i] += 1;
- myStr[j] = myStr[i];
- i -= 1;
- j += 1;
- }
- while(i > -1){
- myStr[j] = myStr[i];
- i -= 1;
- j += 1;
- }
- for(int k=0; k < myStr.size(); k++){
- ss<<myStr[k];
- }
- finalString = ss.str();
- cout<<"\nThe next smallest palindrome is : "<<finalString;
- }
- }
- }
- }
- int main()
- {
- string inputString;
- int temp;
- bool cont = true;
- while(cont){
- cout << "Enter the number : ";
- cin>>inputString;
- nextSmallestPalindrome(inputString);
- cout<<"\nDo you want to continue (1/0) : "<<endl;
- cin>>temp;
- if(temp == 0){
- cont = false;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment