forked from sapai05/cpp-and-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.cpp
More file actions
58 lines (51 loc) · 1.1 KB
/
palindrome.cpp
File metadata and controls
58 lines (51 loc) · 1.1 KB
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
#include<iostream>
#include<cstring>
using namespace std;
/*
This program detects whether an input is a palindrome or not.
Author: Sahil Pai
Date: 9/25/22
*/
char *removeSpaces(char *palindrome)
{
// remove spaces
int i = 0, j = 0;
while (palindrome[i])
{
if (isalnum(palindrome[i])) {
palindrome[j++] = palindrome[i];
}
i++;
}
//convert to lowercase
palindrome[j] = '\0';
int count = 0;
int len = strlen(palindrome);
for (int x = 0; x < len; x++) {
int c = palindrome[x];
if (isalpha(c)) {
count++;
palindrome[x] = tolower(c);
}
}
palindrome[count] = '\0';
//cout<<palindrome<<endl;
return 0;
}
int main () {
char palindrome[80];
char *pal2 = palindrome;
cin.getline (palindrome,80,'\n');
removeSpaces(pal2);
int l = 0;
int h = strlen(pal2)-1;
// check if input is a palindrome
while(h > l){
if(pal2[l++] != pal2[h--]){
cout << "Not a palindrome" << endl;
return 0;
}
}
cout << "Is a palindrome" << endl;
return 0;
}