As salamo alaikum wa rahmatullah
package examples;
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str=sc.nextLine();
int len=str.length();
String rev="";
for(int j=len-1;j>=0;j--) {
rev=rev+str.charAt(j);
}
System.out.println("Reverse string is : -"+ rev);
rev="";
char a[]=str.toCharArray();
int len1=a.length;
for(int k=len1-1;k>=0;k--)
{
rev=rev+a[k];
}
System.out.println("Again the reverse :- "+rev);
// Reverse string using StringBuffer class
StringBuffer sbf=new StringBuffer(str);
StringBuffer rev2=sbf.reverse();
System.out.println("Again the reverse :- "+rev2);
}
}
Jazak Allah Khair
No comments:
Post a Comment