Java way to check if a string is palindrome
You can try something like this :
String variable = ""; #write a string name
StringBuffer rev = new StringBuffer(variable).reverse();
String strRev = rev.toString();
if(variable.equalsIgnoreCase(strRev)) # Check the condition
Check string for palindrome
Why not just:
public static boolean istPalindrom(char[] word){
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
Example:
Input is "andna".
i1 will be 0 and i2 will be 4.
First loop iteration we will compare word[0]
and word[4]
. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).
So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).
Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.
How to check if a string is a palindrome?
Just reverse the string and compare it against the original
string_to_check = input("Enter a string")
if string_to_check == string_to_check[::-1]:
print("This is a palindrome")
else:
print("This is not a palindrome")
Check if a string is a palindrome
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char[] arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
Checking if a string is palindrome
It compares the last character with the first and moves inside the string as follows:
hannah
^ ^
check the two letters are equal: (return False if not)
move the index by 1
hannah
^ ^
Do the same check
hannah
^^
right index = left index at this point
return True in this case
Check if a permutation of a string can become a palindrome
Really all you're looking for is if all (or all but one) of the letters are paired off. As long as they are, then they will be able to be turned into a palindrome.
So it would be something like...
bool canBeTurnedIntoAPalindrome(string drome)
{
// If we've found a letter that has no match, the center letter.
bool centerUsed = false;
char center;
char c;
int count = 0;
// TODO: Remove whitespace from the string.
// Check each letter to see if there's an even number of it.
for(int i = 0; i<drome.length(); i++)
{
c = drome[i];
count = 0;
for(int j = 0; j < drome.length(); j++)
if (drome[j] == c)
count++;
// If there was an odd number of those entries
// and the center is already used, then a palindrome
// is impossible, so return false.
if (count % 2 == 1)
{
if (centerUsed == true && center != c)
return false;
else
{
centerused = true;
center = c; // This is so when we encounter it again it
// doesn't count it as another separate center.
}
}
}
// If we made it all the way through that loop without returning false, then
return true;
}
This isn't the most efficient (it's counting letters as many times as it comes across them, even if they've been counted already) but it does work.
Related Topics
Using R.Net.Community in .Net Core 2.0 Preview 1
How to Ask The Socket to Wait for More Data to Come
How to Add a Custom View Dynamically to a View in Visual Studio for MAC C#
Razor Syntax Error Serializing ASP.NET Model to JSON with HTML.Raw
How to Join 2 or More .Wav Files Together Programmatically
How to Access Resourcedictionary in Wpf from C# Code
How to Open PDF File in a New Tab or Window Instead of Downloading It (Using ASP.NET)
More Elegant Exception Handling Than Multiple Catch Blocks
Why Would C# Processstartinforedirectstandardoutput Cause Xcopy Process to Fail
Getting @@Identity from Tableadapter
How to Get Float Value with SQLdatareader
How to Validate That a String Doesn't Contain HTML Using C#
Hmc Sha1 Hash - C# Producing Different Hash Output Than Ruby