public class StringReverse {
public static void main(String[] args) {
String str = "Paras";
loopReverse(str);
usingCollection(str);
usingStringBuilder(str);
}
// using reverse() of StringBuilder
static void
usingStringBuilder(String str) {
StringBuilder
strReverse = new StringBuilder();
strReverse.append(str);
strReverse.reverse();
System.out.println(strReverse);
}
// using charAt() of String class...
static void loopReverse(String str) {
String strReverse = "";
int length = str.length();
for (int i = length; i > 0; --i) {
strReverse = strReverse + str.charAt(i - 1);
}
System.out.println(strReverse);
}
// Using reverse() of Collections Class...
static void
usingCollection(String str){
List<Character> listChar= new LinkedList();
char[] charString = str.toCharArray();
for(char c:charString)
listChar.add(c);
Collections.reverse(listChar);
Iterator itr=listChar.iterator();
while(itr.hasNext())
System.out.print(itr.next());
System.out.println();
}
}
Output:
saraP
saraP
saraP
Output:
saraP
saraP
saraP
No comments:
Post a Comment