-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl34.cpp
More file actions
42 lines (42 loc) · 894 Bytes
/
l34.cpp
File metadata and controls
42 lines (42 loc) · 894 Bytes
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
#include<iostream>
using namespace std;
using namespace std;
bool checkPalindrome(string str, int i, int j){
if(i>j){
return false;
}
if(str[i]!=str[j]){
return false;
}
else{
//Recursive call
return checkPalindrome(str, i+1, j-1);
}
}
void reverse(string &str, int i , int j){
//base case
if(i>j){
return;
}
swap(str[i], str[j]);
i++;j--;
reverse(str, i, j);
}
void reverseWithoutRecursion(string &str, int i, int j){
while(i<=j){
swap(str[i], str[j]);
i++; j--;
}
}
int main(){
string name = "babbar";
cout<<endl;
bool isPalindrome = checkPalindrome(name, 0, name.length()-1);
if(isPalindrome){
cout<<"It is a Palindrome";
}
reverse(name, 0, name.length()-1);
cout<<name;
reverseWithoutRecursion(name, 0, name.length()-1);
cout<<name;
}