My program is reading from a file that has the following lines (plus quite a few more....) the name of the txt file is palindromes.txt.

Able was I ere I saw elba
Go hang a salami I'm a lasagna hog
Dennis and Edna sinned
Satan oscillate my metallic sonatas
12325” is not a palindrome
2222
a  
A%^()!2a
"Zeus: "Nile macaroni, Ma, is a nitrate-tart in Asia Minor, a camel in Suez."
Can I attain a 'C'?

For some reason the program is returning that all of those are NOT palindromes. But some of them are. Can someone take a look and tell me why they are all coming back false? Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
ifstream fin;  
ofstream fout; 
bool PalindromeChecker(string inputString)
{
    int i=0; 
    
    int length = inputString.length();
    char temp[255] = {'\0'};
 
    for (i=0; i<length; i++)
    {
        
        if ((temp[i] >= 'a') && (temp[i] <= 'z'))
            temp[i] = char(int(temp[i]) - 32);
    }
    for (i=0; i<length; i++)
    {
        
        if ((temp[i] < 'A') || (temp[i] > 'Z'))
        {
            for (int j=i; j<length; j++) 
            {                            
                temp[j] = temp[j+1];     
            }
            i--;      
            length--; 
        }
    }
    
    int checkLength = length - 1;
   
    if ((checkLength == 0) || (checkLength == 1)) 
        return true;
    for (i=0; i<=checkLength/2; i++) 
    {
        if (temp[i] != temp[checkLength-i]) 
            return false; 
    }
    return true; 
} 
void printAnswer(string input, bool isit) 
{ 
    if (isit == true) 
        cout << input.data() << " is a palindrome." << endl; 
    else 
        cout << input.data() << " is not a palindrome." << endl; 
}
int main()
{
    string inputString;          
    bool isPalindrome = true;    
    fin.open("palindromes.txt"); 
    fout.open("output.txt");     
    if (!fin.good())        
        throw "I/O Error!"; 
    if (!fout.good())       
        throw "I/O Error!"; 
    while (fin.good())
    { 
        
        getline(fin,inputString);
       
        isPalindrome = PalindromeChecker(inputString);
      
        printAnswer(inputString,isPalindrome);
    }
    fin.close();
    fout.close(); 
    return 0; 
}

THANKS!!
confused!

Recommended Answers

All 5 Replies

are you doing A level computing by any chance?
I am and are doing the same thing, except in VB

Hmm... something looks fishy:

char temp[255] = {'\0'};
 
    for (i=0; i<length; i++)
    {
        
        if ((temp[i] >= 'a') && (temp[i] <= 'z'))
            temp[i] = char(int(temp[i]) - 32);

Why are you checking the value of temp right after you give initalize it with \0 ? Perhaps in the if() statement you wanted to use inputString instead?

Hope this helps

How are you defining a palindrome? Obviously, it means that the string is symmetric about its middle, but how do you handle non-alphabetic characters? For instance, if you check that every char is between 'a' and 'z', you'll miss out on capital letters (easily resolved with the tolower(char) function found in <cctype> ), but you also miss numbers and special characters. The last of those is probably good to skip, but numbers you may want to keep (you do have them in your test file, afterall).

If I were to write this, I would implement a loop which kept 2 indices into inputString. One would start from the back, and one from the front. For each one, I would find the first alphanumeric value ( isalnum(char) ) that hasn't been checked. Compare the two and either return false if they don't match or continue checking until the two indices cross each other. If they do cross, return true.

Oh, before I forget: isalpha and isalnum both return non-zero (i.e. true) if the given char is a letter or letter-or-number, respectively.

Hi! someone else had a problem like yours. you can see my solution for palindrom writen in java.visit the JAVA tab, that may help you

Regards,Andrijana

I'm sure he's been waiting around for 4 months just hoping you'll give him a solution. :rolleyes:

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.