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.
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 string contains palindromes separated by a hash symbol using a vector as an implementation of the stack
A few hints for the first version.
The
while(hashSymbol && !stack.empty()){
Is superfluous. Check just one character at a time.
stack.push_back(s);
pushes the '#' on the stack too.
I think you will have an easier time to adapt your second version rather than correcting the first.
Your not far away from a solution..
Related Topics
What Is a Non-Trivial Constructor in C++
Memset() or Value Initialization to Zero Out a Struct
How to Use If (Pointer) Instead of If (Pointer != Null)
Getting Libcurl to Work with Visual Studio 2013
How to Write Custom Input Stream in C++
Any Reason to Overload Global New and Delete
Why Do You Use Std::Move When You Have && in C++11
Passing a Std::Array of Unknown Size to a Function
Writing Universal Memoization Function in C++11
Difference Between Std::System_Clock and Std::Steady_Clock
C++: Simple Return Value from Std::Thread
Long Long Int VS. Long Int VS. Int64_T in C++
What Are the Rules for the "..." Token in the Context of Variadic Templates
How to Project a Point Onto a Plane in 3D
String Conversion with Boost Locale: Different Behaviour on Windows and Linux