C Palindrome String Program


A palindrome is a string that reads the same forward and backward, ignoring spaces, punctuation, and capitalization. In C, you can write a program to check whether a given string is a palindrome by comparing characters from the start and end of the string, moving towards the center. Below is an explanation and implementation of a palindrome string program in C.

Implementation of Palindrome String Program

Here is a simple implementation of a palindrome check in C:

#include <stdio.h> #include <string.h> #include <ctype.h> // Function to check if a string is a palindrome int isPalindrome(char str[]) { int left = 0; // Start pointer int right = strlen(str) - 1; // End pointer // Loop until pointers meet in the middle while (left < right) { // Ignore non-alphanumeric characters while (left < right && !isalnum(str[left])) left++; while (left < right && !isalnum(str[right])) right--; // Compare characters (case insensitive) if (tolower(str[left]) != tolower(str[right])) { return 0; // Not a palindrome } // Move towards the center left++; right--; } return 1; // It is a palindrome } int main() { char str[100]; // Array to hold the input string // Ask user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read input including spaces str[strcspn(str, "\n")] = 0; // Remove the newline character if present // Check if the string is a palindrome if (isPalindrome(str)) { printf("The string is a palindrome.\n"); } else { printf("The string is not a palindrome.\n"); } return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: Includes standard input-output functions.
    • #include <string.h>: Includes string manipulation functions.
    • #include <ctype.h>: Includes functions for character classification.
  2. Function Definition:

    • int isPalindrome(char str[]): This function checks if the input string is a palindrome.
    • Parameters: It takes a character array (string) as input.
  3. Variables:

    • left: Pointer starting from the beginning of the string.
    • right: Pointer starting from the end of the string.
  4. While Loop:

    • The loop runs as long as left is less than right.
    • The inner while loops ignore non-alphanumeric characters using the isalnum function.
    • Characters at the left and right indices are compared using tolower to ensure the comparison is case-insensitive.
  5. Character Comparison:

    • If the characters at the left and right indices do not match, the function returns 0 (indicating it is not a palindrome).
    • If they match, both pointers move towards the center.
  6. Return Value:

    • If the loop completes without finding any mismatches, the function returns 1, indicating the string is a palindrome.
  7. Main Function:

    • An array str is declared to hold the input string.
    • The user is prompted to enter a string using fgets to include spaces.
    • The strcspn function removes any newline character from the string.
    • The palindrome check function is called, and the result is displayed.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc palindrome.c -o palindrome
  2. Execute the Program:

    ./palindrome
  3. Input Data: Enter a string when prompted.

Example Input/Output

Input:

Enter a string: A man, a plan, a canal, Panama

Output:

The string is a palindrome.

Conclusion

This palindrome string program in C checks for palindromes by considering only alphanumeric characters and ignoring cases. It demonstrates how to manipulate strings, use character classification functions, and implement logic using loops and conditional statements.