Checking if a given String is a Palindrome or not in C

Here’s a step-by-step guide and a simple C program to check if a given string is a palindrome:

#include <stdio.h>
#include <string.h>

// Function to check if the string is a palindrome
int isPalindrome(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;

    while (start < end) {
        if (str[start] != str[end]) {
            return 0; // Not a palindrome
        }
        start++;
        end--;
    }
    return 1; // Is a palindrome
}

int main() {
    char str[100]; // Declare a string of size 100

    // Input the string
    printf("Enter a string: ");
    scanf("%s", str);

    // Check if the string is a palindrome
    if (isPalindrome(str)) {
        printf("The string '%s' is a palindrome.\n", str);
    } else {
        printf("The string '%s' is not a palindrome.\n", str);
    }

    return 0;
}

Detailed Explanation

  • Include Headers: We include the necessary header files. stdio.h for standard input/output functions and string.h for string functions.
#include <stdio.h>
#include <string.h>
  • Palindrome Check Function: We define a function isPalindrome that takes a string as input and checks if it is a palindrome.
int isPalindrome(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;

    while (start < end) {
        if (str[start] != str[end]) {
            return 0; // Not a palindrome
        }
        start++;
        end--;
    }
    return 1; // Is a palindrome
}
  • We initialize two indices, start and end, pointing to the beginning and end of the string.
  • We use a loop to compare characters from the start and end of the string. If any characters don’t match, the function returns 0, indicating that the string is not a palindrome.
  • If all characters match, the function returns 1, indicating that the string is a palindrome.

  • Main Function: In the main function, we handle user input and call the isPalindrome function.
int main() {
    char str[100]; // Declare a string of size 100

    // Input the string
    printf("Enter a string: ");
    scanf("%s", str);

    // Check if the string is a palindrome
    if (isPalindrome(str)) {
        printf("The string '%s' is a palindrome.\n", str);
    } else {
        printf("The string '%s' is not a palindrome.\n", str);
    }

    return 0;
}
  • We declare a character array str to store the input string.
  • We use scanf to read the input string. Note that scanf will read a single word (i.e., it stops reading at a space).
  • We call the isPalindrome function to check if the string is a palindrome.
  • We print the result based on the return value of the isPalindrome function.

Testing the Program

When you run the program, it will prompt you to enter a string. For example:

Enter a string: radar
The string 'radar' is a palindrome.

Or:

Enter a string: hello
The string 'hello' is not a palindrome.

Conclusion

Checking if a string is a palindrome is a fundamental exercise for beginners to understand string manipulation and basic conditional statements in C programming. This program provides a simple and clear way to check if a string is a palindrome by comparing characters from the start and end of the string. Feel free to leave your questions or comments below if you need further assistance or clarification. Happy coding!

Leave a Comment

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

Scroll to Top