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
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
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.)
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...
Write a menu driven Java program which uses a method for each of the following operations:...
Write a menu driven Java program which uses a method for each of the following operations: (Note : The user should be allowed to repeat the operations as long as he wants to. Use appropriate number of parameters and return type for each method.) A. to find the sum of the following series (up to N terms). The program should    display the terms:              22 + 42 + 62… For example, if N=4, then the program should display the following...
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
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT