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.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");
}
}
}
Comments
Post a Comment