Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Write a Java program to reverse a string

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program to reverse a string

    import java.util.Scanner;
     
    class RicMain
    {
    	public static void main(String args[])
    	{
    		{
    	        Scanner rc = new Scanner(System.in);
     
    	        System.out.print("Input a string: ");
    	        char[] letters = rc.nextLine().toCharArray();//When inputting, what happens then?
     
    	        System.out.print("Reverse string: ");
    	        for (int i = letters.length - 1; i >= 0; i--)//What is happening inside the for loop? 
    	        {
    	            System.out.print(letters[i]); 
    	        }
    	        System.out.print("\n");
    	    }
    	}
    }

    Please help me understand this code. I had to find the solution online because I didn't know where to begin, so I got this code, tried it on my Java and it worked, but I have trouble understanding it. So After I input my name, how does the for loop reverse it?
    I know rc.nextLine() is where I input characters, but when I add toCharArray(), does it convert that array into a string or something?
    Please explain how toCharArray() works.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Write a Java program to reverse a string

    explain how toCharArray() works.
    Have you read the API doc for that method?
    Here's a link to the Java SE API doc: http://docs.oracle.com/javase/8/docs/api/index.html
    Find the class in lower left window, click on it and its doc will show in the main window.

    If you have questions about the doc, copy its contents and paste it here with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to reverse a string

    toCharArray() basically converts a string into an array, right? (I did not look at the link you showed me yet)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Write a Java program to reverse a string

    Yes, If you have questions about what a method does, the API doc is the best place to start.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Write a program to reverse each word in line(sentence)?
    By snehasishmohanta@gmail.co in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 4th, 2014, 09:38 AM
  2. Write a program to reverse each word in line(sentence)?
    By snehasishmohanta@gmail.co in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 3rd, 2014, 11:34 PM
  3. Replies: 9
    Last Post: February 26th, 2013, 10:36 AM
  4. how to write a function *method* in Java to reverse an Array using Groovy?
    By Cyclist in forum Other Programming Languages
    Replies: 7
    Last Post: October 17th, 2012, 06:20 PM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM