Reversing a String

Write a program to input a string(str). Reverse the string and store it in another string(rev). Print both the strings and also a message whether the strings are same or not.




SOURCE CODE


import java.io.*;
import java.util.*;
public class check_two_String
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner (System.in);
        System.out.print("\nEnter a String    :");
        String str=sc.nextLine();
        String rev="";
        for(int i=str.length()-1;i>=0;i--)
        {
            char ch=str.charAt(i);
            rev=rev+ch;
        }
        System.out.print("\nGiven String      ="+str);
        System.out.print("\nReverse String    ="+rev);
        String rev2=rev;
        if(rev2.equals(str))
        {
            System.out.println("\nYes,Both String are equal");
        }
        else
        {
            System.out.println("\nNo,Both String are not equal");
        }
    }

}


OUTPUT




If you have any question then leave a comment below I will do my best to answer that question.

Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.