Check if a Given String is a Palindrome in C

 

Introduction: A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example, the word “madam” or the phrase “A man, a plan, a canal, Panama!” is a palindrome. In this program, we will check if a given string is a palindrome or not.

Objective: The objective of this program is to take a string input from the user, and then determine whether the string is a palindrome. This will be done by comparing the string with its reverse, character by character. If both match, the string is a palindrome.

Code Implementation in C

#include 
#include 
#include 

int isPalindrome(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;

    while (start < end) {
        // Skip non-alphanumeric characters
        if (!isalnum(str[start])) {
            start++;
        } else if (!isalnum(str[end])) {
            end--;
        } else {
            // Compare characters case-insensitively
            if (tolower(str[start]) != tolower(str[end])) {
                return 0; // Not a palindrome
            }
            start++;
            end--;
        }
    }
    return 1; // Is a palindrome
}

int main() {
    char str[100];

    // Input string from the user
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    
    // Remove trailing newline character from fgets
    str[strcspn(str, "\n")] = 0;

    // 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;
}

Program Explanation

The program is structured in the following way:

  • Function Definition (isPalindrome): This function takes a string as an argument and checks whether it is a palindrome. It uses two pointers, start and end, to compare characters from both ends of the string moving towards the center. It also ignores non-alphanumeric characters and compares the characters in a case-insensitive manner.
  • Main Function: The main function prompts the user to input a string and stores it in a character array. Then, it calls the isPalindrome function to check if the string is a palindrome and displays the result.

Steps to Run the Program:

  1. Step 1: Copy the provided C code into a C programming editor or IDE (e.g., Code::Blocks, DevC++, or Visual Studio Code with C extension).
  2. Step 2: Compile the program. If using a terminal, you can use the following command:
    gcc -o palindrome palindrome.c
  3. Step 3: Run the compiled program. In the terminal, you can run it as:
    ./palindrome
  4. Step 4: Enter a string when prompted and check the result on the console.

Conclusion

This simple program allows you to easily check if a string is a palindrome by comparing characters from both ends. By ignoring non-alphanumeric characters and making the comparison case-insensitive, it ensures that phrases like “A man, a plan, a canal, Panama!” are also recognized as palindromes.

 

Leave a Reply

Your email address will not be published. Required fields are marked *