Java Program to determine whether a given string is palindrome
In this program, we need to check whether a given string is palindrome or not. A string is said to be palindrome if it is the same from both the ends. 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.
string 1
"mama"
string 2
"kaka"
Program
class String_pallidrome {
public static void main(String[] args) {
//declare string
String str1="kaka";
boolean flag= true;
str= str.toLowerCase();
for(int i=0;i< (str.length())/2;i++) {
if (str.charAt(i) != str.charAt(str.length()-i-1))
{
flag=false
break;
}
if(flag= true ){
System.out.println( "pallidrome" );
}
else {
System.out.println( "not pallidrome" );
}
}
}
output
pallidrome
Theory
in this program , we need to check whether a string is palindrome or not. A string is said to palindrome then string backword write is some to write forword otherwise it is not a palindrome string
we are to travel loop to midle of the string to compare the first char to last char then increase the value of i an use flag is boolean varible declare is true but after travelling loop flag is rename true it is palindrome string as basis of our programming method other wise you are use various method as ssson possible to you.
No comments:
Post a Comment