Friday, 20 April 2018

//wap to reverse the String?

public class StringReverse {


String reverse(String str)
{
  for(int i=str.length()-1;i>=0;i--)
{
System.out.print(str.charAt(i));
}
return str;

}


public static void main(String[] args) {

StringReverse sr=new StringReverse();
sr.reverse("My name is khan");
}


}
===============================================================================
public class StringReverse1 {


public StringBuffer reverse(String s)
{
StringBuffer sb=new StringBuffer(s);
sb.reverse();
System.out.println(sb);
return sb;
}

public static void main(String[] args) {
StringReverse1 sr1=new StringReverse1();
sr1.reverse("My name is Khan");
}



}
==========================================================================


public class StringReverse2 {



public StringBuilder reverse(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
System.out.println(sb);
return sb;
}

public static void main(String[] args) {
StringReverse2 sr1=new StringReverse2();
sr1.reverse("My name is Khan");
}



}
==============================================================================

public class ReverseString3 {

char[ ] Reverse(String str)
{
char[] c1 = str.toCharArray();
for(int i=c1.length-1;i>=0;i--)
{
System.out.print(c1[i]);
}
return c1;

}

public static void main(String[] args) {
ReverseString3  rs3=new ReverseString3();
rs3.Reverse("My name is khan");
}

}
==============================================================================

import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class StringReverse4 {

ListIterator<Character> Reverse(String str)
{
char[] c1 = str.toCharArray();
List<Character> al=new ArrayList<>();
for(char c:c1)
al.add(c);
Collections.reverse(al);
ListIterator<Character> li=al.listIterator();
        while (li.hasNext())
        {
      System.out.print(li.next());
        }
        return li;

}
public static void main(String[] args) {

StringReverse4 sr4=new StringReverse4();
sr4.Reverse("My name is Khan");

}
}
===============================================================================

public class StringReverse5 {
char [ ] reverse(String r)
{
char[] c1 = r.toCharArray();
int i=0;
int j;

char ch;
for(j=c1.length-1;j>i;i++,j--)
{
ch=c1[j];
c1[j]=c1[i];
c1[i]=ch;
}

for(char c:c1)
{
System.out.print(c);
}
return c1;
}

   public static void main(String[] args) {
StringReverse5 sr5=new StringReverse5();
sr5.reverse("My name is Khan");
}}

No comments:

Post a Comment