Computer Programming question (java)?

How would I, using methods, take a string and print the REVERSE of it. I'm clueless where to go...if anyone could help, it would be greatly appreciated!

Prakash2008-03-27T05:10:49Z

Favorite Answer

public static void main(String[] args) {
String s = "Arun";
int i = s.length() - 1;
for (; i >= 0; i--) {
System.out.print(s.charAt(i));
}
}


Learn Java From This web site

http://www.javabeginner.com/

titobeau2008-03-27T05:04:03Z

You could take a string and use a loop to put the characters into an array, then put the characters back into a string backwards using a different loop. I would check Javadocs to see if there isn't already some method that can do this.

many rawe2008-03-29T16:12:51Z

You can do it this way in C++ using pointers. I am pretty sure you will easily adapt it to java.. Hope i helped exactly what u want... and i hope i solved your question by writing this program using pointers..


#include <iostream.h>
#include <conio.h>

void main()
{
char ch[100],*p, ct[100], *tp;

p=ch;
cout<<"enter string"<<endl;
*p=cin.get();
while(*p!='$')
{
p++;
*p=cin.get();
}

*p='\0';

p--;
tp=ct;
int i=0;

while(p>=ch)
{
*tp=*p;
p--;
tp++;
}

*tp='\0';

cout<<"Reverse Array"<<endl;

tp=ct;

while(*tp!='\0')
{
cout.put(*tp);
tp++;
}

cout<<endl;
getch();
}

i found some java related questions at http://previouspapers.blogspot.com/2008/03/java-technical-hr-interview-questions.html

Anonymous2008-03-27T05:26:16Z

here is your code

import java.util.Scanner;

public class ReverseString{

public static String reverse(String str)
{
String temp="";
for(int i=str.length()-1;i>=0;i--)
temp+=str.charAt(i);
return temp;
}
public static void main(String a[])
{
System.out.print("Enter the string to reverse : ");
Scanner input = new Scanner(System.in);
String s=(String)input.next();
s=reverse(s);
System.out.println("Reversed string :"+s);
}
}

sample output

E:\tli\ya-help>java ReverseString
Enter the string to reverse : hello
Reversed string :olleh