C++ palindrome Code

How to check the string in one file is palindrome or not? Print how many strings in that file are palindrome?

Showing Answers 1 - 1 of 1 Answers

king003

  • Jun 3rd, 2008
 

//Ranier De Leon
//June 2008

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

void tryAgain(int);
void input();
void main()
{
    input();
}
void tryAgain(int a)
{
    char TA;
    cout<<"Try Again?[Y/N]:";
    cin>>TA;
    cin.ignore();
    try
    {
        if(TA == 'y' || TA == 'Y')
            main();
        else if(TA == 'n' || TA== 'N')
            exit(1);
        else
            throw TA;
    }
    catch(char a)
    {
        cout<<"Y or N only!! not "<<a<<endl;
        tryAgain(a);
    }

}

void input()
{
    char word[20];
    string word2, wordrev;
    int cnt = 0, cnt2 =0;
    cout<<"This Program checks if the word entered is a Palindrome or not..."<<endl;
    cout<<"Enter a word or phrase:";
    cin.getline(word, 20);

    for(int i = 0; i<=20; i++)
    {
        word[i] = char(toupper(word[i]));
    }
    word2 = word;
    wordrev = strrev(word);

    do
    {
        cnt++;
        if(isspace(word2[cnt]))
        {
            word2.erase(cnt, 1);
            cnt--;
        }        
        if(isspace(wordrev[cnt]))
        {
            wordrev.erase(cnt, 1);
            cnt--;
        }
        if(isdigit(word2[cnt]))
        {
            word2.erase(cnt, 1);
            cnt--;
        }
        if(ispunct(word2[cnt]))
        {
            word2.erase(cnt, 1);
            cnt--;
        }
        if(isdigit(wordrev[cnt]))
        {
            wordrev.erase(cnt, 1);
            cnt--;
        }
        if(ispunct(wordrev[cnt]))
        {
            wordrev.erase(cnt, 1);
            cnt--;
        }    
    }while(cnt<20);


        if(word2 == wordrev)
        {
            cout<<"The word or phrase "<<word2<<" is a Palindrome"<<endl;
            tryAgain(cnt);
        }
        else
        {
            cout<<"The word or phrase "<<word2<<" is not a Palindrome"<<endl;
            tryAgain(cnt);
        }


}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions