Question 1- Write a java program to perform the following on any given string:
1. To find its length
2. To convert it to uppercase
3. To Check whether it is a Palindrome or not.
solution -
import java.util.Scanner;
class first_2015
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String str = sc.nextLine();
String reverse = "";
int length = str.length();
System.out.println("Length of String: "+length);
System.out.println("Uppercase of String: "+str.toUpperCase());
for(int i=length-1;i>=0;i--)
{
reverse += str.charAt(i);
}
if(str.equals(reverse))
{
System.out.println("String is Palindrome");
}
else
{
System.out.println("String is not Palindrome");
}
}
}
Explanation - We are importing the scanner package so that we can the input from the user. length() method of String class is used to get the length of string. toUpperCase() method is used to convert the string into Uppercase. String.equals(string2) return true if both string are same otherwise it return false.
output:
Enter the String: Ignoulia
Length of String: 8
Uppercase of String: IGNOULIA
String is not Palindrome
Length of String: 8
Uppercase of String: IGNOULIA
String is not Palindrome
Enter the String: abcba
Length of String: 5
Uppercase of String: ABCBA
String is Palindrome
Length of String: 5
Uppercase of String: ABCBA
String is Palindrome
if you have any doubt related to this post, you can ask in comment section and if you want solution of any previous year questions. please post that question in section we will try to solve that
0 comments:
Post a Comment