Palindrome Checker

Palindrome Checker

Easy C Strings 32 views
Explanation Complexity

Problem Statement

Check if a string is a palindrome. Case-insensitive and ignore spaces. Example: "a man a plan a canal Panama" is a valid palindrome.

Input Format

A string entered by the user.

Output Format

YES if the string is a palindrome, otherwise NO.

Example

"a man a plan a canal Panama"
YES

Constraints

• Case-insensitive

• Spaces must be ignored

Concept Explanation

A palindrome reads the same forward and backward.
Here, spaces are ignored and letters are compared without case difference.

Step-by-Step Explanation

1.Take the input string.

2.Set two pointers:

• start at the beginning

• end at the end

3.While start < end:

4.If start points to a space:

• Move start forward.

5.If end points to a space:

• Move end backward.

6.Convert both characters to lowercase.

7.Compare characters at start and end:

• If they are not equal → not a palindrome.

8.If equal:

• Move start++ and end--.

9.If loop finishes without mismatch:

• The string is a palindrome.

Concept Explanation

A palindrome reads the same forward and backward.
Here, spaces are ignored and letters are compared without case difference.

Step-by-Step Explanation

1.Take the input string.

2.Set two pointers:

• start at the beginning

• end at the end

3.While start < end:

4.If start points to a space:

• Move start forward.

5.If end points to a space:

• Move end backward.

6.Convert both characters to lowercase.

7.Compare characters at start and end:

• If they are not equal → not a palindrome.

8.If equal:

• Move start++ and end--.

9.If loop finishes without mismatch:

• The string is a palindrome.

Input / Output Format

Input Format
A string entered by the user.
Output Format
YES if the string is a palindrome, otherwise NO.
Constraints
• Case-insensitive

• Spaces must be ignored

Examples

Input:
"a man a plan a canal Panama"
Output:
YES

Example Solution (Public)

C
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[200];
    int left = 0, right = 0;
    int isPalindrome = 1;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Find length
    while (str[right] != '�') {
        right++;
    }
    right--;   // last character index

    // Remove newline if present
    if (str[right] == 'n') {
        right--;
    }

    // Two-pointer comparison
    while (left < right) {

        if (str[left] == ' ') {
            left++;
            continue;
        }

        if (str[right] == ' ') {
            right--;
            continue;
        }

        if (tolower(str[left]) != tolower(str[right])) {
            isPalindrome = 0;
            break;
        }

        left++;
        right--;
    }

    if (isPalindrome)
        printf("The string is a palindrome.n");
    else
        printf("The string is NOT a palindrome.n");

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <ctype.h>

int main() {
    char str[200];
    int left = 0, right = 0;
    int isPalindrome = 1;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Find length
    while (str[right] != '�') {
        right++;
    }
    right--;   // last character index

    // Remove newline if present
    if (str[right] == 'n') {
        right--;
    }

    // Two-pointer comparison
    while (left < right) {

        if (str[left] == ' ') {
            left++;
            continue;
        }

        if (str[right] == ' ') {
            right--;
            continue;
        }

        if (tolower(str[left]) != tolower(str[right])) {
            isPalindrome = 0;
            break;
        }

        left++;
        right--;
    }

    if (isPalindrome)
        printf("The string is a palindrome.n");
    else
        printf("The string is NOT a palindrome.n");

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.