TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Determine Whether a Given String is Palindrome

Program to Determine Whether a Given String is Palindrome on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to determine whether a given string is Palindrome

Explanation

In this program, we need to check whether a given string is palindrome or not.

Kayak

A string is said to be palindrome if it reads the same backward as forward. For e.g. above string is a palindrome because if we try to read it from backward, it is same as forward. One of the approach to check this is iterate through the string till middle of string and compare a character from back and forth.

Algorithm

  1. Define a string.
  2. Define a variable flag and set it to true.
  3. Convert the string to lowercase to make the comparison case-insensitive.
  4. Now, iterate through the string forward and backward, compare one character at a time till middle of the string is reached.
  5. If any of the character doesn't match, set the flag to false and break the loop.
  6. At the end of the loop, if flag is true, it signifies string is a palindrome.
  7. If flag is false, then string is not a palindrome.

Solution

Python

string = "Kayak";
flag = True; 
 
#Converts the given string into lowercase
string = string.lower();
 
#Iterate the string forward and backward, compare one character at a time 
#till middle of the string is reached
for i in range(0, len(string)//2):
    if(string[i] != string[len(string)-i-1]):
        flag = False;
        break;
 
if(flag):
    print("Given string is palindrome");
else:
    print("Given string is not a palindrome");

Output:

Given string is palindrome

C

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
 
int main()
{
    char string[] = "Kayak";
    bool flag = true;
    
    //Converts the given string into lowercase
    for(int i = 0; i < strlen(string); i++){
        string[i] = tolower(string[i]);
    }
    
    //Iterate the string forward and backward, compare one character at a time 
    //till middle of the string is reached
    for(int i = 0; i < strlen(string)/2; i++){
        if(string[i] != string[strlen(string)-i-1]){
            flag = false;
            break;
        }
    }
    
    if(flag)
        printf("Given string is palindrome");
    else
        printf("Given string is not a palindrome");
        
    return 0;
}

Output:

Given string is palindrome

JAVA

public class Palindrome
{
    public static void main(String[] args) {
        String string = "Kayak";
        boolean flag = true;
        
        //Converts the given string into lowercase
        string = string.toLowerCase();
        
        //Iterate the string forward and backward, compare one character at a time 
        //till middle of the string is reached
        for(int i = 0; i < string.length()/2; i++){
            if(string.charAt(i) != string.charAt(string.length()-i-1)){
                flag = false;
                break;
            }
        }
        if(flag)
            System.out.println("Given string is palindrome");
        else
            System.out.println("Given string is not a palindrome");
    }
}

Output:

Given string is palindrome

C#

using System;
                    
public class Palindrome
{
    public static void Main()
    {
        String string1 = "Kayak";
        Boolean flag = true;
        
        //Converts the given string into lowercase
        string1 = string1.ToLower();
        
        //Iterate the string forward and backward, compare one character at a time 
        //till middle of the string is reached
        for(int i = 0; i < string1.Length/2; i++){
            if(string1[i] != string1[string1.Length-i-1]){
                flag = false;
                break;
            }
        }
        if(flag)
            Console.WriteLine("Given string is palindrome");
        else
            Console.WriteLine("Given string is not a palindrome");
    }
}

Output:

Given string is palindrome

PHP

<!DOCTYPE html>
<html>
<body>
<?php
$string = "Kayak";
$flag = true;
 
//Converts the given string into lowercase
$string = strtolower($string);
 
//Iterate the string forward and backward, compare one character at a time 
//till middle of the string is reached
for($i = 0; $i < strlen($string)/2; $i++){
    if($string[$i] != $string[strlen($string)-$i-1]){
        $flag = false;
        break;
    }
}
if($flag)
    print("Given string is palindrome");
else
    print("Given string is not a palindrome");
?>
</body>
</html>

Output:

Given string is palindrome

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf