Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include <stdio.h>
#include <ctype.h>
#define STR_LEN 80

int main()
{
    char s[STR_LEN+1];
    int i = 0;
    char *left;
    char *right;
    char *end;

    for (i = 0; i< = STR_LEN; i++) 
    {
        s[i] = ' ';
    }
    printf("Enter: ");
    fgets(s, STR_LEN+1, stdin);

    left = &s[0];
    end = right = &s[STR_LEN];

    while (left<right) 
    {
        while ((!((*left>=65)&&(*left<=90)))&&
               (!((*left>=97)&&(*left<=122)))&&
               (left<end)) 
        {
            left++;
        }
        while ((!((*right>=65)&&(*right<=90)))&&
               (!((*right>=97)&&(*right<=122)))&&
               (right>s)) 
        {
            right--;
        }
        if (left == end) 
        {
            break;
        }
        if (tolower(*left) != tolower(*right)) 
        {
            printf("\nNo Palindrome!\n\n");
            return 0;
        }
        left++; 
        right--;
  }

  printf("\nPalindrome!\n\n");
  return 0;
}
Posted
Updated 20-Dec-11 11:59am
v5
Comments
Philippe Mori 20-Dec-11 18:30pm    
By the way, a debugger can explain the code step by step.

1 solution

It's starting at each end of the character array, looking for character matches. Given the string "abccba", it's comparing the character at position 0 and 5, 1 and 4, and 2 and 3 to see if they're the same. If they are, it's a palindrome. If it runs into a pair that doesn't match, it's not a palindrome. Technically, it should not be case-sensitive, but that's a matter of implementation.

BTW, I change the formatting a little so it's easier to see how it's working.
 
Share this answer
 
v2

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