Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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!

4 Answers

Relevance
  • 1 decade ago
    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/

  • 1 decade ago

    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.

  • 1 decade ago

    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-te...

  • Anonymous
    1 decade ago

    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

Still have questions? Get your answers by asking now.