Palindrome issue

This is a discussion on Palindrome issue within the C++ Programming forums, part of the General Programming Boards category; I'm having an issue with my palindrome program. I believe it's only determining if the first letter matches the last. ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Palindrome issue

    I'm having an issue with my palindrome program. I believe it's only determining if the first letter matches the last. I assume this because the entries in my string "race car" and "race casr" both claim to be palindromes. I'm looking for a fix for this:

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    #include<string>
    
    
    int main()
    {
        int i=0;
        char str[100], c;
    
    
        cout<<"Enter a word/words"<<endl;
        if(str[i] == ' ')
        {
            str[i]='\b';
        }
        cin.getline(str, 100);
        int s=strlen(str)-1;
        while(str[i])
        {
            c=str[i];
            putchar(tolower(c));
            i++;
        }
        for(int y=0; y<1; y++)
        {
            if(str[y]==str[s-y])
            {
                cout<<""<<endl;
                cout<<"The word(s) "<<str<<" is a palindrome"<<endl;
            }
            else
            {
                cout<<""<<endl;
                cout<<"The word(s) "<<str<<" is not a palindrome"<<endl;
            }
        }
        getch();
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    You have written a rather unhelpful for loop. y needs to increase as s decreases or anything mathematically equivalent.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The <string> header is for C++ std::string objects (which you should consider using anyway instead of C style null-terminated char arrays). The proper header for functions such as strlen would be <cstring>.



    Code:
    if(str[i] == ' ')
    {
        str[i]='\b';
    }
    This code is pointless, the data in str is uninitialized at this point in time.




    Code:
    while(str[i])
    {
        c=str[i];
        putchar(tolower(c));
        i++;
    }
    Are you trying to convert all the letters in the user entered string to lower case prior to the comparison loop? All you are doing is outputting to the console, you aren't actually changing the data in str. You probably also want to overwrite the data in str with the lowered chars at this point to ensure your later comparison works regardless of the case entered by the user. This would also be the place to remove any non-alphabetic characters.



    Code:
    #include<conio.h>
    
    ...
    
    getch();
    There's no need to be using non-standard headers/functions here. You can do without these and simply use a call (or 2) to cin stream's get member function.



    Code:
    using namespace std;
    #include<string>
    Avoid including any headers after a using namespace std declaration. This is just as risky as putting such a line inside of a header file itself. That line should be after the include, not before it.



    Your loop comparison, as already mentioned, is flawed. If you got the indexing issue resolved it still looks like you would be outputting a "yes it's a palindrome" or "no it's not a palindrome" message for every character comparison instead of just once.

    Your two index variables - y, s - needs to both be modified within the loop. y should increase by 1 and s should decrease by 1.

    The loop condition should stop once y is equal to or greater than s. The loop should also stop once it's determined that the string entered by the user cannot possibly be a palindrome due to an unequal char comparison. At that point you would not need to continue making comparisons since we already would know it is not a palindrome.

    The loop should alter a flag variable of some kind - bool perhaps, initialized to true. Once a comparison is made that is unequal, you can set the flag variable to false and exit the loop. If no unequal comparisons are made then the flag variable remains unchanged - true.

    After the loop you then test the flag variable for true/false and output an appropriate message.



    You should try learning more about the std::string container and the various stl functions available to you in C++. The std::transform stl function can help you convert everything in the user entered string to lower-case. The std::remove_if function can help get rid of any non-alphabetic characters so you just have a string with a-zA-Z characters. The std::equal function can make the test for palindrome a one-liner. I'm sure there are examples of the use of these functions available here and elsewhere out there on "teh interwebz".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. palindrome
    By jollykiddo in forum C Programming
    Replies: 28
    Last Post: 12-17-2011, 09:11 AM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. the palindrome
    By haochao in forum C Programming
    Replies: 15
    Last Post: 10-11-2008, 11:29 PM
  4. palindrome
    By chodmama in forum C Programming
    Replies: 1
    Last Post: 02-20-2006, 01:52 AM
  5. palindrome
    By nightingale in forum C Programming
    Replies: 2
    Last Post: 07-26-2003, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21