i was able to convert the string into case insensitive and remove special characters but now all i end up with is the same output - it is a palindrome.
please rectify the code and let me know how to fix it.
Code:
#include<iostream>
//We are creating a string and 2 stacks. Then we push each char of the string into the stack and pop it into an another and compare them.
using namespace std;
struct Stack {
char *arr;
int size;
int capacity;
int top;
Stack(int n) {
capacity = n;
top = 0;
size = 0;
arr = new char;
}
char pop() {
if (size == 0) {
cout << "Stack empty" << endl;
return '0';
}
char ch = arr[0];
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
return ch;
}
void display() {
if (size == 0) {
cout << "Stack empty" << endl;
return;
}
cout << "[";
for (int i = 0; i < size - 1; i++) {
cout << arr << ",";
}
cout << arr[size - 1] << "]" << endl;
}
void push(int data) {
if (size == capacity) {
cout << "Stack full" << endl;
return;
}
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = data;
size++;
return;
}
};
int main()
{
cout << "Enter letters/words/numbers/phrases :" << endl;
std::string x;
std::string temp;
do {
getline(cin, x); //input string
}
while (x.empty()); // removing spaces
x.erase((remove(x.begin(), x.end(), ' ')), x.end());
std::transform(x.begin(), x.end(), x.begin(),::tolower); // transform tolower
for (int i = 0; i < x.size(); ++i) {
if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) //initializing condition
{
temp = temp + x; //passing characters that match condition to temp string from initialized string
}
}
cout << temp;
int n = x.size();
Stack stack(n / 2);
int i = 0;
for (i = 0; i < n / 2; i++) {
stack.push(x);
}
if (n % 2 != 0) {
i++;
}
while (i < x.size()) {
if (stack.pop() != x) {
cout << "The word/phrase/number is not a palindrome" << endl;
break;
}
i++;
}
if (i == x.size()) {
cout << "The word/phrase/number is a palindrome" << endl;
}
}