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 given string is palindrome c++ iterative method
You defined an empty object of the type std::string
string temp;
So you may not use the subscript operator with the empty object in the loop.
int k = 0;
for(int i = length-1; i>=0; i--){
temp[++k] = str[i];
}
Using your approach you could just write
string temp( str.rbegin(), str.rend() );
without using a loop.
However to check whether a string is a palindrome there is no need to create an intermediate string.
You could do it in a loop the following way.
std::string::size_type i = 0;
for ( auto n = str.length(); i < n / 2 && str[i] == str[n - i - 1]; )
{
++i;
}
if ( i == str.length() /2 ) std::cout << str << " is a palindrome\n";
Or without the loop and defining one more variable you could write
if ( str == std::string( str.rbegin(), str.rend() ) )
{
std::cout << str << " is a palindrome\n";
}
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
Type List VS Type Arraylist in Java
How to Put a Control in the Jtableheader of a Jtable
How to Calculate Someone'S Age in Java
What Does the ^ Operator Do in Java
Properly Removing an Integer from a List≪Integer≫
How to Hash a Password in Java
Trust Store VS Key Store - Creating With Keytool
Hibernate Throws Multiplebagfetchexception - Cannot Simultaneously Fetch Multiple Bags
What Causes Javac to Issue the "Uses Unchecked or Unsafe Operations" Warning
How to Parse Command Line Arguments in Java
How to Implement a Tree Data-Structure in Java
How to Remove Objects from an Array in Java
Org.Xml.Sax.Saxparseexception: Content Is Not Allowed in Prolog
Simplest Way to Read Json from a Url in Java