Check whether a string is PALINDROME or Not | FACE Prep

Check whether a string is PALINDROME or Not | FACE Prep

Check if a String is a Palindrome: A Complete Guide

A palindrome is a word, phrase, number, or any sequence of characters that reads the same forward and backward. Examples include “radar,” “madam,” “level,” and “racecar.”

In this article, we’ll explore how to check whether a given string is a palindrome using a simple algorithm and an efficient program.

What is a Palindrome?

A string is considered a palindrome if:

  • The original string and its reverse are identical.
  • It remains unchanged even if read from right to left.

Algorithm to Check if a String is a Palindrome

To determine whether a string is a palindrome, follow these steps:

  1. Input the string.
  2. Reverse the string.
  3. Compare the original and reversed strings:
    • If both are equal, return true (it’s a palindrome).
    • Otherwise, return false (it’s not a palindrome).

Code Implementation: Check for a Palindrome

Python Program to Check for a Palindrome

# Function to check if a string is a palindrome
def is_palindrome(s):
    return s == s[::-1]  # Reverse and compare

# Input from the user
string = input("Enter a string: ")

# Check if palindrome
if is_palindrome(string):
    print("The given string is a palindrome.")
else:
    print("The given string is not a palindrome.")

C Program to Check for a Palindrome

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

// Function to check for palindrome
int is_palindrome(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            return 0; // Not a palindrome
        }
    }
    return 1; // It's a palindrome
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);
    
    if (is_palindrome(str)) {
        printf("The given string is a palindrome.\n");
    } else {
        printf("The given string is not a palindrome.\n");
    }
    return 0;
}

C++ Program to Check for a Palindrome

#include <iostream>
#include <algorithm>
using namespace std;

bool isPalindrome(string str) {
    string rev = str;
    reverse(rev.begin(), rev.end());
    return str == rev;
}

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;
    if (isPalindrome(str))
        cout << "Palindrome";
    else
        cout << "Not a Palindrome";
    return 0;
}

Java Program to Check for a Palindrome

import java.util.Scanner;

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        String rev = new StringBuilder(str).reverse().toString();
        return str.equals(rev);
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = scanner.next();
        
        if (isPalindrome(str))
            System.out.println("Palindrome");
        else
            System.out.println("Not a Palindrome");
    }
}

Recommended Programs to Enhance Your Skills

  • Palindrome Number Checker: Check if a number (e.g., 121, 1331) is a palindrome.
  • Basic String Operations: Learn string manipulation techniques.
  • Find String Length Without strlen(): Implement custom logic to find string length.
  • Toggle Each Character in a String: Convert uppercase to lowercase and vice versa.
  • Count Vowels in a String: Determine the number of vowels in a given string.
  • Remove Vowels from a String: Modify strings by eliminating vowels.

Conclusion

Checking for a palindrome is a fundamental concept in programming. The approach varies slightly between languages, but the logic remains the same—reverse and compare. By practicing different variations of palindrome programs, you’ll strengthen your problem-solving skills and string manipulation expertise.

Check if a String is a Palindrome
c