C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given string is PalindromeExplanationIn 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
SolutionPythonstring = "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 JAVApublic 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#
|