Hello im kinda of new to java and i am stuck on a program. I want to take a word as input and display it in reverse.

ex - hello to olleh

I tried to do the For loop but i am kinda of stuck. Can anyone please help me! thanks!

do a While loop, where you say, while the word is not olleh, do... then you make a loop in which you rearrange the letters one by one, starting from the last one.

isn't a for loop easier? how do i make it to be the reverse of the user input?

import java.util.Scanner;


public class Backwardsword
{

    public static void main(String[] args)
    {
    String word="";
    int j;
    char[] chr;


    System.out.println("Enter a Word:");
    Scanner keyboard = new Scanner(System.in);
    word= keyboard.nextLine();

    chr= word.toCharArray();


    for (j=word.length()-1; j>=0; j--)

    System.out.println("The word in reverse is " + (chr[j]) + ".");
    }
}

I ran my program but the output is

the word in reverse is o.
the word in reverse is l.
the word in reverse is l.
the word in reverse is e.
the word in reverse is h.

well what you got is the word in reverse. is your question why the output is 5 different lines instead of one line saying olleh? if so, look at your System.out.println and you should see it. otherwise, i'm not sure what your question is, because you got the expected output.

I want the user to input a word, example happy
and i want the program to output on the screen, yppah

your problem is in the last for loop, because you are telling the program to output it character by character by putting the system.out.println each time. you need one system.out.println which will output the entire word

Either use a loop like others are saying or you can take the lazy way out:

String sText = "bracket";
String sReverse = new StringBuilder(sText).reverse().toString();
System.out.println(sReverse);

:) A StringBuilder contains a reverse method. Then you reverse it and convert the StringBuilder back to a string with the toString() method. :)

i slightly modified my program and came out with this

import java.util.Scanner;
public class Backwardsword
{
    public static void main(String[] args)
    {
    String word="";
    System.out.println("Enter a Word:");
    Scanner keyboard = new Scanner(System.in);
    word= keyboard.next();

    for (int j=word.length(); j >=0; j--)
    {
    System.out.println(word.substring(j-1, j));
    }
    }
}

I have one error. i run the program and it says index out of range.i dunno what do do! help!

Whatsup,

I'm sure at this point you know how to read the input from the keyboard.

Here's the reversal code. Later.

-- LiveWire

public class StringReversal
{
	
	public static void main(String args[])
	{
	String str= "", str2 = "you're the man";
	
	char ch[] = str2.toCharArray();
	
		
	for(int i = ch.length-1; i > -1; i--)
	{
		str += "" + ch[i];
	}
	
	System.out.println(str);
	System.out.println("Program over");
	
	}
	
	
	
}

@curtissumpter: what do you think you are doing giving people ready made code, this forum boasts an attitude opposite of that. Tell the OP the logic mentioning the way to go about, or at the most the psuedocode, but not written code in any manner.

@peedi : Use a stack to reverse the word and I am sure it will be the most convenient approach. Just keep pushing all the letters till the end of the word and then pop them back one by one.

^ That's a very good solution, I was just going to suggest going through the String in reverse order and printing the current character.

@curtissumpter: what do you think you are doing giving people ready made code, this forum boasts an attitude opposite of that. Tell the OP the logic mentioning the way to go about, or at the most the psuedocode, but not written code in any manner.

@peedi : Use a stack to reverse the word and I am sure it will be the most convenient approach. Just keep pushing all the letters till the end of the word and then pop them back one by one.

yup stacks are easiest for that
push it all in the stack and pop n print it

Why use a stack? You could just loop through the string and print each char in reverse order.

But still think using the built in StringBuilder is better.

for (int i=s.length()-1;i>=0;i--) {
      System.out.print(s.charAt(i));
}
System.out.println();

Basically you start at the last character of the string and loop through each character in the String printing in reverse.

yup stacks are easiest for that
push it all in the stack and pop n print it

But chances are if a person doesn't know how to use str.toCharArray(), they are not going to be familiar with various data structures. If that's the case you could suggest a doubly linked list or whatever, but at the level of the original poster it would seem kind of absurd. However, I do take your point of the original poster (not writing full code) however previous to my post there were others that were almost completely full code, they were just buggy. I simply clarified what was prior.

-- LiveWire

Why use a stack? You could just loop through the string and print each char in reverse order.

Because they seem logical, don't they, you just run through the string in a forward order as apart from a backward order and then just pop out elements and they are reversed. After all Stacks are made for such use.

But chances are if a person doesn't know how to use str.toCharArray(), they are not going to be familiar with various data structures.

Absolutely wrong. str.toCharArray() is langauge specific thing, whereas stacks arent, not to mention they are the most basic of the data structures that are taught to beginners. Also since they do not "belong" to any language the chances that a beginner knows them are quite good than he knowing something like str.toCharArray() which is a Java specific method.

If that's the case you could suggest a doubly linked list or whatever

Wrong again. I haven't heard doubly link lists being use for string reversal anytime. ;)

We'll have to agree to disagree. You are getting into teaching methods and styles saying that a data structure would be taught before a basic object. I know I wasn't taught data structures until my third course in computer science where as classes and methods (including built in methods) were taught in courses one and two. (Beginning programming and Advanced Programming). But maybe you were taught differently.

And almost any data structure could be used to reverse a string. A variety of trees. A doubly linked list. A stack. So to say "absolutely" when it is not absolute is absolutely wrong.

We'll have to agree to disagree. You are getting into teaching methods and styles saying that a data structure would be taught before a basic object. I know I wasn't taught data structures until my third course in computer science where as classes and methods (including built in methods) were taught in courses one and two. (Beginning programming and Advanced Programming). But maybe you were taught differently.

And almost any data structure could be used to reverse a string. A variety of trees. A doubly linked list. A stack. So to say "absolutely" when it is not absolute is absolutely wrong.

Firstly, I didn't ever say that you are not taught classes and methods before data structures, I said, that one would certainly not be taught the str.toCharArray method before data structures if you understand that. Knowing specifics reqiures deeper understanding than generics thats the simple point here. Not to mention I am sure no class teaches the String class' methods in Java so a beginner is certainly not supposed to know that.

Also I never said that doubly linked lists cannot be used to reverse strings, you can write a program that uses that damn structure to reverse a string, there can be thousands of different implementations for a single thing and many of them can be absolutely unfitting in the sense that they won't solve the problem in the most optimized, logical way, I said that I haven't heard of they being used for such purpose. You can, for that matter, carry out a poll asking people what is a better DS to be used for string reversal ? Stack/Doubly linked list and then find out that 99.99% of them (all the sane people) would go for a stack.

I have to disagree with you Curtis. A "Data Structures" course typically assumes that you already have a knowledge of things like stacks and queues, which are generally taught in lower level courses. Not to rehash this argument. And in my opinion, any solution to the problem is helpful, as long as you explain the advantages and disadvantages of each. So there's nothing wrong with telling him how to do it with a doubly linked list as long as he's aware that a stack is probably the more logical way to do it.

Okay, breathe guys. Breathe.

Just let it go.

-- Curtis

I want the user to input a word, example happy
and i want the program to output on the screen, yppah

println break the line for output instead use print and display only the array...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.