Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 2 February 2019

SECOND WEEK TERM WORK Program 3 (1a)

Question:

 Write a Java Program that takes a string as command-line argument and checks whether it is a palindrome or not. If the string is a palindrome, then it must print its length and convert that string from lower case to upper case letters or vice versa. If the string is not a palindrome, then
it should generate a user-defined exception StringNotPalindromeException.


Solution:
class StringNotPalindromeException extends Exception{
public StringNotPalindromeException(String message) {
super(message);
}

}
public class Exceptionpal {
public static void main(String[] args) {
char cmdline[]=args[0].toCharArray();
int n=args[0].length(),j=0,i=0;
try {
for(i=n-1;i>=n/2;i--,j++)
{
if(cmdline[i]!=cmdline[j])
{
throw new StringNotPalindromeException("String are not Palindrome");
}
}
}
catch(Exception e)
{
System.out.println("Error:");
System.out.println(e.getMessage());
}
if(i<j)
{
System.out.println("String is Palindrome");
System.out.println("String length "+args[0].length());
System.out.println("String In Lower Case:"+args[0].toLowerCase());
System.out.println("String In Upper Case:"+args[0].toUpperCase());
}
}

}

0 comments:

Post a Comment