Coder Social home page Coder Social logo

testtesttest's Introduction

TASK:

Write a Java program that takes a string as input and checks whether it is a palindrome or not. A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward
Also Test your program with different input strings to ensure it works correctly.

INPUT SAMPLE:

"level" is a palindrome.
"java" is not a palindrome.

SOLUTION:

package prac1;

import java.io.*;
import java.util.Scanner;

class t102 {
	
    public static void main (String[] args) {
    	
    	System.out.print("Enter Your Strings: ");
        Scanner in = new Scanner(System.in); 
        String input = in.nextLine();   //input data from user & store on 'input' variable
        String output = ""; //'output' variable initialize it with 0
        
        for (int i = input.length()-1; i >= 0; i--) {
        	 output = output + input.charAt(i); //reverse the string & stored new data in 'output' variable 
         }
         
        if (input.toLowerCase().equals(output.toLowerCase())) {//compare the reversed string
        	System.out.println(input+" is a palindrome");
           } 
        else {
        	System.out.println(input+" is not a palindrome!");
        	}    
/*
1. for loop ➔  To get the characters of the input in the reverse order.
2. input.length()-1 ➔ Because characters in the string are indexed from 0.
3. input.charAt(i) ➔  Returns character value to the index of the string and store it in 'output'.
4. toLowerCase() ➔  converts input, output to lowercase because java is case sensitive.
5. equals() ➔  checks if two strings are equal. 
*/
        
         in.close(); //close Scanner
    }
}
      

TEST PORGRAM WITH DIFFERENT INPUT:

1 2 3


#Happy_Coding


testtesttest's People

Contributors

ehasan101 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.