string using java??please help..the problem is..inputting your name then reverse it.

Recommended Answers

All 18 Replies

What have you tried? Please post the code showing where you are and ask about any problems you are having.

import java.util.*;


public class ReverseThis
{


    static Scanner console = new Scanner(System.in);    
    public static void main(String []args)

    {


        String s; //Declare a variable string
        String x=""; //Initialize the variable x.

        System.out.print("Enter name:");

        s=console.nextLine();//Accept the input...


        System.out.print(s + "\n");//Print the inputted string.

        for(int i=s.length()-1;i>=0;i--)//This is how we reverse the inputted string.
        {

            char cc = s.charAt(i);

            x+= Character.toString(cc);

        }
    }
}

Next time, show to us what you have done so far....So that many will help you...
I do understand since you are still a beginner on this java and i know you are confuse but i am pretty sure that you have ideas how to do this but cannot express in codes...

Note...There are different ways in reversing the Array try to learn and read the java docs that will help you....and lastly if you are really love java don't stop reading books and practicing some exercises from basic to advance...

Hope this will help you.

@NormR1, I do apologize of this. But how can we help the OP (I am reffering to the first time learning in java)...The OP is still getting to know in java,he wants to know what is String and how to manipulate it,Methods and Classes...and i know the OP don't understand yet the API docs in java...

It takes a lot more effort to help an OP learn how to program than writing some code. Just posting code is NOT the best way.

This is how we reverse the inputted string.

But you neglect to explain how and why the code works. How did you come up with technique you used in the code? A little pseudo code to start with, giving the OP a chance to try it himself first.

@NormR1,Okay maybe next time i will use it pseudo code to let the OP giving a chance to code by himself.

Pseudo code can be a useful tool for explaining algorithms etc, but by doing that you have still deprived the OP of the chance to learn how to solve the logical problem for themselves. Follow Norm's frequent approach of getting the OP to do it by hand on a piece of paper, then generalise what they just did, before going anywhere near a computer.

also, NeiXude, Strings can't be "manipulated", they're immutable.
make sure you explain correctly what you are doing.

reversing string using array.

example of the out :
enter name:
paul
reverse:
luap

Simple code only please??thankyou in advance.

use toCharArray to read your String into a char array
iterate over them using a simple for loop, after you created a new array of chars of the same size
set the first element of the first array in the last element of the second array
set the second element of the first array in the just not last element of the second array
...
set the last element of the first array in the first element of the second array

yes I am a beginner in Java and I am only 2ndyear college student.

@stultuske can you please create a sample program please?? thankyou.

have you had java both these years?

have you had java both these years?

n

Look at using two arrays: one the source the other the target. Have the index to one point to the source and the index to the other point to the target. Copy an element from where the source index points to where the target index points. Change the indexes to point to the next elements, test if at the end. If not go do it again.

I can create a 'sample program' but that's basically giving you the code. try looking at the pseudo code I gave you, see if you can find the components to use to do that, and decide on the steps, try and write that, and post that here.

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.