Program to Check if Given String is Palindrome or Not

Program to Check if Given String is Palindrome or Not

  • Write a program to Check if Given String is Palindrome or Not in C
  • Write a program to Check if Given String is Palindrome or Not in C++
  • Write a program to Check if Given String is Palindrome or Not in Python
  • Write a program to Check if Given String is Palindrome or Not in PHP
  • Write a program to Check if Given String is Palindrome or Not in Java
  • Write a program to Check if Given String is Palindrome or Not in Java Script
  • Write a program to Check if Given String is Palindrome or Not in C#

Explanation:

Finding out if a particular string reads the same way backwards as it does forwards is the first step in determining whether it is a palindrome.

Algorithm:

  1. Start with the original string.
  2. (Optional) Normalize the string:
    • Remove non-alphanumeric characters.
    • Convert to a consistent case.
  3. Initialize two pointers:
    • One pointing to the beginning of the string.
    • The other pointing to the end of the string.
  4. Compare the characters at the two pointers:
    • If they match, move the pointers inward.
    • If they do not match, the string is not a palindrome.
  5. If all characters match, the string is a palindrome.

Program to Check if Given String is Palindrome or Not

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

int main() {
    char str[100];
    int start, end, length;
    
    // Scan the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    
    // Remove newline character
    str[strcspn(str, "\n")] = '\0';
    
    // Calculate length of the string
    length = strlen(str);
    
    // Check for palindrome
    start = 0;
    end = length - 1;
    while (start < end) {
        if (str[start] != str[end]) {
            printf("The string is not a palindrome.\n");
            return 0;
        }
        start++;
        end--;
    }
    
    printf("The string is a palindrome.\n");
    return 0;
}

#include <iostream>
#include <string>

int main() {
    std::string str;
    
    // Scan the string
    std::cout << "Enter a string: ";
    std::getline(std::cin, str);
    
    // Check for palindrome
    int start = 0, end = str.length() - 1;
    while (start < end) {
        if (str[start] != str[end]) {
            std::cout << "The string is not a palindrome." << std::endl;
            return 0;
        }
        start++;
        end--;
    }
    
    std::cout << "The string is a palindrome." << std::endl;
    return 0;
}

str1 = input("Enter a string: ")

# Check for palindrome
if str1 == str1[::-1]:
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

<?php
// Scan the string
echo "Enter a string: ";
$str = trim(fgets(STDIN));

// Check for palindrome
if ($str == strrev($str)) {
    echo "The string is a palindrome.\n";
} else {
    echo "The string is not a palindrome.\n";
}
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Initialize scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        // Scan the string
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();
        
        // Check for palindrome
        String reversedStr = new StringBuilder(str).reverse().toString();
        if (str.equals(reversedStr)) {
            System.out.println("The string is a palindrome.");
        } else {
            System.out.println("The string is not a palindrome.");
        }
    }
}

let str = prompt("Enter a string: ");

// Check for palindrome
let reversedStr = str.split('').reverse().join('');
if (str === reversedStr) {
    console.log("The string is a palindrome.");
} else {
    console.log("The string is not a palindrome.");
}

using System;

class Program {
    static void Main() {
        // Scan the string
        Console.Write("Enter a string: ");
        string str = Console.ReadLine();
        
        // Check for palindrome
        string reversedStr = new string(str.ToCharArray().Reverse().ToArray());
        if (str == reversedStr) {
            Console.WriteLine("The string is a palindrome.");
        } else {
            Console.WriteLine("The string is not a palindrome.");
        }
    }
}

List of All Programs