Java Program to Check Palindrome String
class PalindromeString
{
public static void main(String[] args)
{
String str = "darga", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i)
{
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase()))
{
System.out.println(str + " is a Palindrome String.");
}
else
{
System.out.println(str + " is not a Palindrome String.");
}
}
}
Output :
darga is not a Palindrome String.
Explanation :
1. class PalindromeString:
2. This is a Java class named "PalindromeString."
3. public static void main(String[] args):
4. This is the main method where the program starts executing.
5. String str = "darga", reverseStr = "";:
6. It initializes two strings, str and reverseStr.
7. str contains the input string "darga," which you can change to test other strings.
8. reverseStr is initially empty and will store the reverse of the str string.
9. int strLength = str.length();:
10. This line calculates the length of the input string str and stores it in the variable strLength.
11. for (int i = (strLength - 1); i >=0; --i):
This is a for loop that iterates through the characters of the input string in reverse order.
It starts from the last character (index strLength - 1) and goes in reverse until index 0.
reverseStr = reverseStr + str.charAt(i);:
Inside the loop, it appends each character from the original string str to the reverseStr string. This effectively reverses the order of characters.
12. if (str.toLowerCase().equals(reverseStr.toLowerCase())) {:
13. After reversing the string, this condition checks whether the original string str and the reversed string reverseStr are equal.
14. str.toLowerCase() and reverseStr.toLowerCase() are used to perform a case-insensitive comparison by converting both strings to lowercase.
System.out.println(str + " is a Palindrome String.");:
15. If the condition is true, meaning that the string is a palindrome, it prints a message indicating that the string is a palindrome.
else { System.out.println(str + " is not a Palindrome String."); }:
16. If the condition is false, indicating that the string is not a palindrome, it prints a message indicating that the string is not a palindrome.
17. So, when you run this program with the input string "darga," it will print "darga is not a Palindrome String." because "darga" is not a palindrome (it does not read the same forwards and backwards). You can change the value of str to test other strings for palindromes.
2. This is a Java class named "PalindromeString."
3. public static void main(String[] args):
4. This is the main method where the program starts executing.
5. String str = "darga", reverseStr = "";:
6. It initializes two strings, str and reverseStr.
7. str contains the input string "darga," which you can change to test other strings.
8. reverseStr is initially empty and will store the reverse of the str string.
9. int strLength = str.length();:
10. This line calculates the length of the input string str and stores it in the variable strLength.
11. for (int i = (strLength - 1); i >=0; --i):
This is a for loop that iterates through the characters of the input string in reverse order.
It starts from the last character (index strLength - 1) and goes in reverse until index 0.
reverseStr = reverseStr + str.charAt(i);:
Inside the loop, it appends each character from the original string str to the reverseStr string. This effectively reverses the order of characters.
12. if (str.toLowerCase().equals(reverseStr.toLowerCase())) {:
13. After reversing the string, this condition checks whether the original string str and the reversed string reverseStr are equal.
14. str.toLowerCase() and reverseStr.toLowerCase() are used to perform a case-insensitive comparison by converting both strings to lowercase.
System.out.println(str + " is a Palindrome String.");:
15. If the condition is true, meaning that the string is a palindrome, it prints a message indicating that the string is a palindrome.
else { System.out.println(str + " is not a Palindrome String."); }:
16. If the condition is false, indicating that the string is not a palindrome, it prints a message indicating that the string is not a palindrome.
17. So, when you run this program with the input string "darga," it will print "darga is not a Palindrome String." because "darga" is not a palindrome (it does not read the same forwards and backwards). You can change the value of str to test other strings for palindromes.
Comments
Post a Comment