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?

**Please clean your code and use correct indentation when posting**

Your teacher is correct. I don't really understand your question.

First, unless you need a take the original string and turn all the letters into upper-case, I see no reason why you would need more than one string for this whole program. For instance, you can count the letter occurrences this way:

int tot = 0, alpha[26];
  
  for(int i = 0 ; i < 26 ; i++)
    alpha[i] = 0;
  
  cout << "Please enter a text: " << endl;
  getline(cin, txt);
  cout << "\n";

  for (int pos = 0 ; pos < txt.length() ; pos++)
  {
    char tmp_c = toupper(txt[pos]); //use a temporary that is only upper-case.
    if(tmp_c >= 'A' && tmp_c <= 'Z') {
      alpha[ tmp_c - 'A' ]++; //use 'A' instead of 65, it is less confusing this way.
      tot++; //why not increase 'tot' at the same time..
    };
  };

The way you "ignore spaces and punctuations" is by simply skipping them as you traverse the string. In other words, instead of incrementing the index by 1 at every iteration, you increment it enough to find the next letter character. As, for example, if you want to print only the letter characters, you could do:

for( int i = 0; i < txt.length(); i++ ) {
    char tmp_c = toupper( txt[i] );
    while(tmp_c < 'A' || tmp_c > 'Z')
      tmp_c = toupper( txt[++i] );
    cout << txt[i];
  };
  cout << endl;

Finally, for the palindrome test, you should not need to reverse the string and then compare. All you need to do is traverse the string both ways at the same time. In other words, start with an index to the last character in the array and an index to the first character, where the former goes backwards in the string (decrementing it) and the latter goes forward (incrementing it). Use the same technique as above to skip non-letters for each index. And then, the string is a palindrome if each pair of letters for each iteration is equal. Stop the iteration when you reach the middle (i.e. when both indexes either point to the same character in the string, or when they have crossed (gone past each other)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.