From 605bbba03d589ce51ef8cdf1a7fc1930c65c125f Mon Sep 17 00:00:00 2001 From: shivali0810 Date: Tue, 25 Oct 2022 21:09:37 +0530 Subject: [PATCH] added cpp code for finding the next palindrome for string inputs --- next_palindrome.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 next_palindrome.cpp diff --git a/next_palindrome.cpp b/next_palindrome.cpp new file mode 100644 index 0000000..ec50dc9 --- /dev/null +++ b/next_palindrome.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; +//finding the next palindrome using brute force + +//to make string symmetric +string mirror(string s){ + int l=s.length(); + int j=0; + for(int i=l-1;i>=l/2;i--){ + s[i] =s[j]; + j++; + } + return s; +} +//to check if middle digits are nine +int nine(string s){ + int l=s.length(); + if(s[l/2]=='9'){ + return 1; + } + else{ + return 0; + } +} +//if middle digits not nine then increase middle digit by 1 +string inc(string s){ + int l=s.length(); + char c=s[l/2]; + int n= c; + n++; + char c1=n; + if(l%2==0){ + s[l/2]=c1; + s[(l/2)-1]=c1; + } + else{ + s[l/2]=c1; + } + return s; +} +//to check if all digit is 9 or not +int allnine(string s){ + int l=s.length(); + int c=0; + for(int i=0;i>t; + while(t--){ + string s; + cin>>s; + string ns=mirror(s); + if(ns>s){ + cout<