- 1
Can we use string buffer object in a loop
Im trying to create a program which takes out each and everyword and checks whether its a palindrome word or not (ps trying to reverse the word using reverse() function)
1 Resposta
0
is this Java? Sure, why not?
StringBuffer p = new StringBuffer("racecar");
for (int I = 0; i < pal.length(); I++) {
//code goes here
// you only have to test half buffer..
}
Or you could just loop through backwards or from each end..
String pal = "racecar";
int right = pal.length()-1;
int left = 0;
public boolean isPalindrome(String p) {
while (left < right) {
if (p.charAt (left) != p.charAt (right))
return false;
else{
left++;
right--;
}
return true;
}