Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<iostream>
using namespace std;

int main( )
{
    char str[80];

    cin.getline(str, 80);
	
    int l;     
    for(l = 0; str[l] != '\0'; l++);

    int i;
    for(i = 0; (i < l/2) && (str[i] == str[l - i - 1]); i++);

    if(i == l/2)
        cout << "Palindrome";
    else
        cout << "Not a palindrome";
	
    return 0;
}


For Example if the word is "gig" then it is palindrome but if the word is "Gig" then it is not working.

Can anyone edit the code so that It can accept both capital and small letter.
Posted
Comments
nv3 23-Jan-16 13:20pm    
Probably not. This task is so incredibly difficult that probably no one else except yourself will be able to do that. So, the world is waiting for you to solve that problem.
Sergey Alexandrovich Kryukov 23-Jan-16 13:22pm    
Start with writing a function "IsAPalindrome". You did not even try it, wrote something in "main".
—SA
Sascha Lefèvre 23-Jan-16 13:22pm    
Take a look at all the functions for string manipulation. There should be one which will convert a string to all upper case or all lower case...
Patrice T 23-Jan-16 13:32pm    
Your homework, not our's

A common way of doing this, is using the tolower() function.

So run the input trough it, and use your algorithm and you are done.
 
Share this answer
 
As you are using a lower-level language, I suggest using a lower-level technique; it should impress your teacher very much.
In the ASCII encoding, the difference between uppercase letters and their lowercase counterparts, is one bit.

For example:

A == 65 == 1000001
a == 97 == 1100001
__________________
XOR  32    0100000


I'm sure you can easily implement a function to test two characters, and if they are letters, then to perform a bit-wise comparison that ignores that one bit.


Your overall implementation will still need to ignore punctuation and such.

Go hang a salami, I'm a lasagna hog!
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 23-Jan-16 14:24pm    
Works just for English. I appreciate it, but it's hard to call a solution for one language (okay, maybe for very few languages) a solution...
—SA
PIEBALDconsult 23-Jan-16 14:26pm    
As I said: "ASCII"
Sergey Alexandrovich Kryukov 23-Jan-16 14:27pm    
I know, you are perfectly right. Anyway, it's important to know. The inquirer should decide what to use.
—SA
Philippe Mori 23-Jan-16 14:27pm    
Seriously, what should we vote for that... In a sense, user get the kind of answer he merit given its question! On the other hand, no one should ever do that in real-life application.
Sergey Alexandrovich Kryukov 23-Jan-16 14:30pm    
I up-voted just for the demonstration of the technique. Of course, as a practical solution it cannot be considered acceptable, that's why I mentioned in my comment above: "it's hard to call a solution"...
—SA
Please see my comment to the question.

And yes, "Gig" is not a palindrome, in the sense you defined it. This is because 'g' != 'G'. If you want a case-insensitive palindrome test, use case-insensitive check-up. Before comparing two characters, convert both to lower case or to upper case. Please see:
tolower - C++ Reference[^],
toupper - C++ Reference[^].

—SA
 
Share this answer
 
Finally I executed by using && statement simply like Either-or statement..

Thanks for your comments.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900