How can I detect a palindrome as complicated as "A man, a plan, a canal, Panama!" without using erase() and just ignore the spaces and punctuations?
The current version is:
#include <stdlib.h> // system pause
//#inlcude <ctype.h> // for toupper()
#include <iostream> // input output
#include <string>
//#include <algorithm>
//#include <iomanip> // for setw() and right-aligned
//#include <math.h> // for pow()
using namespace std;
int main()
{
/* char alpha [26];
char ch;
string txt;
for(int i = 0 ; i <= 25 ; i++)
{alpha [i] = 'A' + i;
cout << alpha [i] << " ";}
cout << "\n";
cout << alpha[4];
cout << "Enter a string: ";
getline(cin, txt);
int max = txt.length();
for (int j = 0 ; j < max ; j++)
txt.at(j);
cout << txt;
*/
/*
int alpha[26];
string txt;
for(int i = 0 ; i < 26 ; i++)
{
alpha[i] = 0;
cout << alpha[i];
}
cout << "\n" ;
cout << "Enter your text: ";
getline(cin,txt);
int max = txt.length();
for (int j = 0 ; j < max; j++)
alpha = txt.at(j);
*/
int alpha[26];
bool palind = true;
for(int i = 0 ; i < 26 ; i++)
alpha[i] = 0;
//string txt = "i love you!";
string txt;
string txt1;
cout << "Please enter a text: " << endl;
getline(cin, txt);
// make upper case of txt
for (int pos = 0 ; pos < txt.length() ; pos++)
{
txt[pos] = toupper(txt[pos]);
// cout << txt.at(pos);
}
cout << "\n";
// ---------------------------------
for (int pos = 0 ; pos < txt.length() ; pos++)
{
if (txt[pos] >= 'A' && txt[pos] <= 'Z')
{
int x = txt[pos] - 65;
alpha [x]++;
}
else
{
//txt[pos] = '\0';
txt.erase(pos); // get rid of punctuations and spaces
// txt [txt.length()-1] = '\0';
}
}
for (int x = 0, y = txt.length()-1; x <= (txt.length()/2),y >=0; y--, x++)
{
txt1+=txt.at(y);
}
//cout << "txt: " << txt << "txt1:" << txt1;
if (txt1 != txt)
{ palind = false;}
if (palind == true)
{cout << "Is a palindrome!";}
cout <<"\n";
int z = 0;
for (z = 0 ; z < 26 ; z++)
cout << char(z + 65) << " " << alpha [z]<< " times in txt " << endl;
system("pause");
return 0;
}
//-------------palindrome
/*
if (txt.length() == 1)
{cout << "Is a palindrome";}
for (x = 0 , y = (txt.length()) - 1 ; x <= txt.length()/2, y >= txt.length()/2 ; x++, y--)
{
if (txt[x] != txt[y])
{
palind = false;
}
}
if (palind == true)
{
cout << "txt is a palindrom";
}
else
{
cout << "it is NOT a palindrome";
}
*/