Simple Palendrome Problem [newbie to C]

This is a discussion on Simple Palendrome Problem [newbie to C] within the C Programming forums, part of the General Programming Boards category; Hi All,
Im trying to write a little programe that checks if a word entered is a palendrome or not.
...

A string is an array of characters followed by a null-terminating 0 (ASCII value 0). So in memory a string looks like r a c e c a r \0

In your loop you're starting at strlen(mystring)-1 which is avoiding the \0 at the end of the loop and that's fine. But after the loop, compstring is left with everything except the \0. The only way functions like strcmp() know how big a string is is by running into that \0 as it's iterating through it. So after your loop you need to add that \0 to the end of compstring. That (along with decrementing the wrong variable in your for loop) should fix your problem.

[edit]Forgot some things, fgets will also extract and store the newline character so allowances must be made to take care of that and also the > in the for loop should be >= so that it copies over an extra character. See changes above in [/edit]

"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

Well, you don't want to set index to 0 at the beginning of every loop iteration, just once before the loop begins. The thing that you're missing is after the loop completes, adding the null terminating zero after all the letters in the compstring array.

"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

[edit]Forgot some things, fgets will also extract and store the newline character so allowances must be made to take care of that and also the > in the for loop should be >= so that it copies over an extra character. See changes above in [/edit]

The first modification can be done simply with strrchr(), included in the string library. What this does is it finds the last occurrence of your character in the string. I started a thread about the easiest way to accomplish this task, though it came down to using strchr() or strrchr().

Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

"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

To debug try printf()'ing mystring and compstring. See if they're really coming up as you expect them to. If they both look right, then the problem is probably in the algorithm that determines if it's a palindrome. If they don't look right, then the problem is in the algorithm that creates compstring.

Also, if the strings look different, seeing how they're different is usually a great indicator of what the problem is.