-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathStringPalindrome.cpp
52 lines (46 loc) Β· 1.08 KB
/
StringPalindrome.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
/* String Palindromr code */
#include <bits/stdc++.h>
using namespace std;
/* Function to test whether a string is palindrome or not */
bool isPalindrome(string S)
{
if (S.length() <= 1)
{
return true; // if length of string is 1 or 0 it will be palindrome for sure
}
// if length of string is more than one we will iterate over a loop
for (int i = 0; i < S.length() / 2; i++)
{
if (S[i] != S[S.length() - i - 1]) // checking characters from first and last of string
{
return false;
}
}
return true;
}
int main()
{
cout << "Enter the string:" << endl; // Input string
string s;
cin >> s;
bool ans = isPalindrome(s); // function call to check a string is palindrome or not
if(ans){
cout << "True" << endl; // true if palindrome
}else{
cout << "False" << endl; // false if not palindrome
}
}
/*
Input:
1. "abba"
2. "abc"
3. "abccad"
Output:
True
False
False
Explanation:
1. abba is a palindrome
2. abc is not a palindrome
3. abccad is not a palindrome
*/