-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp1.cpp
60 lines (56 loc) · 1.69 KB
/
cp1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*Problem Statement:- Take a String value in str variable from keyboard.Find out given string is palindrome or not.
If given string is not palindrome then try to make given string palindrome with minimum addition.
Test-Cases
"Hello" output answer is "hellolleh"
"abb" output answer is "abba"
"aaabb" output answer is "bbaaabb"*/
# include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s)
{
int len=s.length();
for (int i=0;i<len/2;i++)
{
if(s[i]!=s[len-i-1])
return 0;
}
return 1;
}
int main()
{
//Initialising the variables
string str,x,y,rev;
bool val;
int strLen;
cout<<"Enter the input string: ";
getline(cin,str); // Input string
strLen = str.length();
rev=str;
reverse(rev.begin(),rev.end()); // the reverse function returns nothing and directly reverses the string passed
//(call by reference method)
if (isPalindrome(str)==0)// String is not palindrome and is converted to palindrome
{
for(int i=0; i<strLen;++i)
{
x = str.substr(0,i);
reverse(x.begin(),x.end());
x = str+x; // used in if
y = rev.substr(0,i);
y = y+str; // used in else if
if (isPalindrome(x)==1)
{
cout<<"The new Palindrome string with minimum addition is: "<<x;
break;
}// there can be no statements in between if and else as it will lead error so write statements within if-else,prior
// or after the if-else block.
else if(isPalindrome(y)==1)
{
cout<<"The new Palindrome string with minimum addition is: "<<y;
break;// break is done to terminate loop after the minimum addition which makes the string palindrome
}
}
}
else
cout<<"The input string is Palindrome.";
return 0;
}