program for Reverse String in java

/* Program to perform reverse string operation using CharAt method*/

class reverse_string
{
public static void main(String args[ ])
{
String reverse_string1 = “HOTJAVA”;
StringBuffer reverse_string2 = new StringBuffer();
int i,j,len = reverse_string1.length();

System.out.println(“Original String is : “+reverse_string1);
reverse_string2.setLength(len);
for(i=len-1,j=0;i>=0;i–,j++)
reverse_string2.setCharAt(j, reverse_string1.charAt(i));

System.out.println(“Reverse String is : “+reverse_string2);
}
}

Reverse String in java

Reverse String in java

/* Program to perform reverse string operation using append method*/

class reverse_string
{
public static void main(String args[])
{
String reverse_string1 = “Gandhi”;
System.out.println(” Original String is : “+reverse_string1);
System.out.println(” Reverse String is : “+reverseIt(reverse_string1));
}

public static String reverseIt(String source)
{
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);

for (i=(len – 1); i>=0; i–)
dest.append(source.charAt(i));
return dest.toString();
}

}

Download “Reverse String Program” reverse-string-1.txt – Downloaded 5 times – 1 KB

NIRAJ AHER

Founder of Java Engineering Programs. Follow me on Google+ ,Linked in.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *