Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string is a palindrome

Tags:

I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.

How can I do this?

Sample:

public bool getStatus(string myString) {  } 

Example: myString = "ankYkna", so if we break it into two substring it would be: left-part = "ank", right-part = "ank" (after reversal).

like image 478
ankur Avatar asked Mar 20 '12 16:03

ankur


People also ask

How do you identify a palindrome?

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.

How do you check if a string is a palindrome in Python?

casefold() # reverse the string rev_str = reversed(my_str) # check if the string is equal to its reverse if list(my_str) == list(rev_str): print("The string is a palindrome.") else: print("The string is not a palindrome.") The string is a palindrome.

How do you check if a string is palindrome or not in Java?

Create a StringBuffer object by passing the required string as a parameter to the constructor. Reverse the contents of the object using the reverse() method. Convert the StringBuffer object to Sting using the toString() method. Now, compare the String and the reversed one, if true, the given string is a palindrome.

How do you check if a word is a palindrome in C++?

Explanation. Start with initializing a bool as true . This value will be set to false if the word is not a palindrome and will stay true if it is a palindrome. Run a for loop to iterate through half of the string, i.e., from i = 0 to i = lenOfStr/2 .


1 Answers

Just for fun:

return myString.SequenceEqual(myString.Reverse()); 
like image 61
Balazs Tihanyi Avatar answered Sep 28 '22 21:09

Balazs Tihanyi