package com.sls.string;
//Here I am doing in 2types.You can use any one of these.
public class ReverseString {
public void reverse() { //This is using for loop
String str = "chintamani";
for (int i = str.length() - 1; i >= 0; i--) {
String rev = String.valueOf(str.charAt(i));
System.out.print(rev);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "My name is Khan";
StringBuffer bf = new StringBuffer(str);
String s = bf.reverse().toString(); //This is using system define functions
System.out.println(s);
ReverseString re = new ReverseString();
re.reverse();
}
}
//Here I am doing in 2types.You can use any one of these.
public class ReverseString {
public void reverse() { //This is using for loop
String str = "chintamani";
for (int i = str.length() - 1; i >= 0; i--) {
String rev = String.valueOf(str.charAt(i));
System.out.print(rev);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "My name is Khan";
StringBuffer bf = new StringBuffer(str);
String s = bf.reverse().toString(); //This is using system define functions
System.out.println(s);
ReverseString re = new ReverseString();
re.reverse();
}
}
Comments