Welcome back to my blog! In my previous post, you learnt how to print a pattern in Java using Eclipse.
Today you will learn how to check whether a string from terminal is palindrome or not.
Let's get started!
import java.util.Scanner;
public class Pal {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("Enter a string:" );
String Str = Scan.nextLine();
int i = Str.length();
String Rev = "";
for(i = i-1; i >= 0; i --)
{
Rev = Rev + Str.charAt(i);
}
if(Str.equals(Rev))
{
System.out.println("The string is palindrome!");
}
else
{
System.out.println("The string is not palindrome.");
}
}
}
Hurrah!!! You have learnt how to make a Java program which checks a string from terminal whether it is palindrome or not.
Feel like a programmer!!!
If you find this post helpful, please share it with your friends, family and colleagues as much as you can.
Thank you for reading! :)
Today you will learn how to check whether a string from terminal is palindrome or not.
Let's get started!
Step #1. Open Eclipse and choose the directory where you want to save your work.
Step #2. Select File>New>Project as shown in the picture below:
Step #3. Select Java>Java Project.
Then, click Next.Step #4. Enter the name of the project in Project name.
Then click on Finish.Step #5. You will see the name of your project in Package Explorer:
Step #6. You will find a folder named as "src" under the name of your project:
Step #7. Right click on the folder "src">New>Class as demonstrated below:
Step #8. Enter the name of the class as shown below:
Then click on Finish.Step #9. You will see a screen like this:
Step #10. Write the following code:
import java.util.Scanner;
public class Pal {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("Enter a string:" );
String Str = Scan.nextLine();
int i = Str.length();
String Rev = "";
for(i = i-1; i >= 0; i --)
{
Rev = Rev + Str.charAt(i);
}
if(Str.equals(Rev))
{
System.out.println("The string is palindrome!");
}
else
{
System.out.println("The string is not palindrome.");
}
}
}
Step #11. Run the code by clicking on the Run button as shown:
Step #12. If the entered string is palindrome, like lol, madam, level, rotor, etc., you will see the output as follows:
If the entered string is not palindrome, you will see the output as follows:
Hurrah!!! You have learnt how to make a Java program which checks a string from terminal whether it is palindrome or not.
Feel like a programmer!!!
If you find this post helpful, please share it with your friends, family and colleagues as much as you can.
Thank you for reading! :)
Comments
Post a Comment