Q:

Program to determine whether a given string is Palindrome

belongs to collection: String Programs

0

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.

Input:

string = "Kayak"  

Output:

Given string is palindrome.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to determine whether one string is a rotat... >>
<< Program to count the total number of characters in...