Header-C
Header-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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)