Question

In: Computer Science

java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true...

java program:

Please save the program with the name ‘Palindrome.java’

Write a method which return true if the string parameter is a palindrome.

This is the output example:

Lets check if a string is a palindrom

Please give me a string: abc

No, its not a Palindrom

Lets check if a string is a palindrom

Please give me a string: noon

Yes, its a Palindrom

Solutions

Expert Solution

//Program for checking string is palindrome or not.

//method isPalindrome is given which returns true when string is palindrome otherwise false

import java.util.Scanner;

public class Palindrome
{    //method for palindrome checking
    public static boolean isPalindrome(String str)
    {
        if(str.length() == 0 || str.length() == 1)
            // if length =0 OR 1 then it is
            return true;
        if(str.charAt(0) == str.charAt(str.length()-1))
            // initially check for first and last char of String:
            // if they are same then do the same thing for a substring.
            return isPalindrome(str.substring(1, str.length()-1));

        // if its not the case than string is not.
        return false;
    }

    public static void main(String[]args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Lets check if a string is a palindrom");
        System.out.print("Please give me a string:");
        String str = s.nextLine();
        if(isPalindrome(str))
            System.out.println("Yes, its a Palindrom");
        else
            System.out.println("No, its not a Palindrom");
    }
}

/* Output

Lets check if a string is a palindrom                                                                                                                                    Please give me a string:abc                                                                                                                                              No, its not a Palindrom                                                                                                                                                                                                                                                                                 

Lets check if a string is a palindrom                                                                                                                                    Please give me a string:noon                                                                                                                                             Yes, its a Palindrom   

*/                                                                                                                                                                                                                                                                                                                


Related Solutions

Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Write a Java program which asks customer name, id, address and other personal information, there are...
Write a Java program which asks customer name, id, address and other personal information, there are two types of customers, walk-in and credit card. The rates of items are different for both type of customers. System also asks for customer type. Depending upon customer type, it calculates total payment. A credit-card customer will pay 5 % extra the actual price. Use object-oriented concepts to solve the problem. Define as many items and prices as you want. Example Output: Enter Name...
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Problem 2 (Save and Get Info) : Write a program that asks for the user's name,...
Problem 2 (Save and Get Info) : Write a program that asks for the user's name, phone number, and address. The program then saves all information in a data file (each information in one line) named list.txt. Finally, the program reads the information from the file and displays it on the screen  in the following format: Name: User's Name   Phone Number: User's Phone Number   Address: User's Street Address User's City, State, and Zip Code
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT