-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkPalindrome.cpp
59 lines (46 loc) · 961 Bytes
/
checkPalindrome.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
#include <iostream>
#include <vector>
using namespace std;
char toLowerCase(char c){
if(c >= 'a' && c <= 'z'){
return c;
}else {
return c - 'A' + 'a';
}
}
int getLength(char a[]) {
int count = 0;
for (int i = 0; a[i] != '\0'; i++) {
count++;
}
return count;
}
void reverseString(char name[], int len) {
int start = 0;
int end = len - 1;
while (start <= end) {
swap(name[start++], name[end--]);
}
}
bool checkPalindromes(char name[], int len) {
int start = 0;
int end = len - 1;
while (start <= end) {
if ( toLowerCase(name[start]) != toLowerCase(name[end])) {
return false;
} else {
start++;
end--;
}
}
return true;
}
int main() {
char name[20];
cout << "Enter your name: ";
cin >> name;
int len = getLength(name);
reverseString(name, len);
cout << "Your reverse name is: " << name << endl;
cout << "Palindrome: " << checkPalindromes(name, len) << endl;
}