I wrote this:
#include <stdlib.h> // system pause
//#inlcude <ctype.h> // for toupper()
#include <iostream> // input output
#include <iomanip> // setw()
#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 tot = 0, alpha[26];
bool palind = true;
for(int i = 0 ; i < 26 ; i++)
alpha[i] = 0;
//string txt = "i love you!";
string txt,txt1,txt2;
cout << "Please enter a text: " << endl;
getline(cin, txt);
txt1 = txt ; // make a copy of the original text in order not to be ruined by alteration s
// make upper case of txt
for (int pos = 0 ; pos < txt1.length() ; pos++)
{
txt1[pos] = toupper(txt1[pos]);
// cout << txt.at(pos);
}
cout << "\n";
// ---------------------------------
for (int pos = 0 ; pos < txt1.length() ; pos++)
{
if(txt1[pos] >= 'A' && txt1[pos] <= 'Z')
{
int x = txt1[pos] - 65;
alpha [x]++;
}
}
for (int z = 0, x = 9, y = 18; z < 9, x < 18, y < 27; z++, x++, y++)
{
if (y == 26)
{cout << char(z + 65) << setw(10) << alpha [z] << setw(20) <<char(x + 65)<< setw(10) << alpha [x]<< setw(20)<< " " <<setw(10) << " " << endl;
}
else
{
cout << char(z + 65) << setw(10) << alpha [z] << setw(20) <<char(x + 65)<< setw(10) << alpha [x]<< setw(20)<< char(y + 65) <<setw(10) << alpha [y]<< endl;
}
}
cout <<"\n";
for (int z = 0 ; z < 26 ; z++)
tot+= alpha[z];
cout<< "total Letters: " << tot;
for (int pos = 0 ; pos < txt1.length() ; pos++)
{
while ((txt1[pos] < 'A' || txt1[pos] > 'Z') && pos != txt1.length())
{ //txt[pos] = '\0';
txt1.erase(pos, 1); // get rid of punctuations and spaces
// txt [txt.length()-1] = '\0';
}
}
for (int x = 0, y = txt1.length()-1; x <= (txt1.length()/2),y >=0; y--, x++)
{
txt2+=txt1.at(y);
}
//cout << "txt: " << txt << "txt1:" << txt1;
if (txt2 != txt1)
{ palind = false;}
if (palind == true)
{cout << "Is a palindrome!";}
cout <<"\n";
cout << "The original text:" << txt << endl;
cout << "The text without ounctuations and space: " << txt1 << endl;
cout << "The reverse edited text: " << txt2 << 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";
}
*/
My teacher's feedback:
If you want full marks, you should not have to make a copy of the string or erase any part of it. For the lettercount, this should be pretty easy -- just skip over non-letters as you go through the string and uppercase the individual letters as you go. You have a good algorithm for counting, by the way.
For the palindrome, it's a bit more difficult. You will have to check letters starting from the beginning & end of the string as you are doing, but skip over any non-letters. It's tricky, but very efficient if you do it that way. So your way would be worth about half-marks for the palindrome.
Totally, I don't know how can I ignore the spaces and punctuations! Any idea plz?