Question

My program is supposed to test for a palindrome and then print it out in reverse but without characters like '!', ', or '?'. So the entered input "madam I'm adam" would output "madamimadam" with no capitalization, blanks, or punctuation. I was able to write the program but how do you do that other part to take away those characters/capitalization? Also for my array, when the program runs it outputs a bunch of odd characters in between the palindrome and the palindrome in reverse when it prints out. I believe this is because the array is filling in the extra character spaces so how would I fix that as well?

#include<iostream>
#include<string>
using namespace std;

int main()
{
    //Variables and arrays
    int const index = 30;
    char Phrase[index];
    char Reverse[index];
    char* Palindrome = Reverse;
    int i, j;

    cout << "Please enter a sentence to be tested as a palindrome: ";
    cin.getline(Phrase, 30);
    int length = strlen(Phrase);

    bool test = true;

    for(i = 0; i != length/2; i++) //Loops from zero to half of the string
    {
        if(test) // if it is a palindrome so far
        {
            if(Phrase[i] != Phrase[length-i-1]) //To check if the characters match
            {
                test = false;
            }

        }
        else
        {
            break;
        }
    }

    if(test)
    {
        cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
        for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
        {
            *Palindrome = Phrase[j];
        }
        cout << "The phrase and reverse statement is: " << Reverse << endl << endl;
    }
    else
    {
        cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;
    }

    system("Pause");
    return 0;
}
Was it helpful?

Solution 3

#include <iostream>
#include <string>
#include <cctype>
    using namespace std;

int main()
{
//Variables and arrays
int const index = 80;
char Phrase[index];
char NewPhrase[index];
int i, j, k, l;
bool test = true;

//Prompt user for the phrase/word
cout << "Please enter a sentence to be tested as a palindrome: ";
cin.getline(Phrase, 80);

//Make everything lowercase, delete spaces, and copy that to a new array 'NewPhrase'
for(k = 0, l = 0; k <= strlen(Phrase); k++)
{
    if((Phrase[k] != ' ') && (ispunct(Phrase[k]) == false))
    {
        NewPhrase[l] = tolower(Phrase[k]);
        l++;
    }
}

int length = strlen(NewPhrase); //Get the length of the phrase

for(i = 0, j = length-1; i < j; i++, j--)
{
    if(test) //Test to see if the phrase is a palindrome
    {
        if(NewPhrase[i] != NewPhrase[j])
            test = false;
    }
    else
        break;
}

if(test)
{
    cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
    cout << "The Palindrome is: " << NewPhrase << endl << endl;
}
else
    cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;

system("Pause");
return 0;
}

OTHER TIPS

You forgot to add the terminating \0 to Reverse. Add *Palindrome = '\0' after the loop.

Just use separate iterators.

Instead of

for(i = 0; i != length/2; i++)

do

for(i = 0, j = length-1; i < j; i++, j--) 

then for your if statement, you can do something like

if(test) // if it is a palindrome so far
{
    while(!isalpha(Phrase[i]) && i < j) { i++; }
    while(!isalpha(Phrase[j]) && i < j) { j--; }
    if(Phrase[i] != Phrase[j]) //To check if the characters match
    {
        test = false;
    }

this will cause your program to ignore any character than isn't a letter. You can use isalphanum if you want it to recognize numbers as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top