Results 1 to 7 of 7
  1. #1
    JavaS is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default How to reverse a string using while loops?

    Hey guys, first time poster here.

    I'm having trouble finding a way to reverse a string that the user inputs using only while loops(we have only covered while and if).

    String s= input.nextLine();



    Any help from someone would be greatly appreciated.

    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Loops can be used to count down as well as up. Set a variable to some value, loop while it is greater than (or equal to) zero, use methods of String class inside loop to do what you need. I have deliberately left a few details out so you can think for yourself how to do it.

  3. #3
    JavaS is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Okay I'll work on this now and I will get back when I have something.

    Thanks

  4. #4
    JavaS is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    So this is what I have come up with.

    Java Code:
     import java.util.*;
    class CharacterArray{
    
    	public static void main(String[]args)
    
    	{
    		Scanner input = new Scanner(System.in);
    
    
    
    
    		String s=input.nextLine();
    
    		char[] stringArray;
    
    		stringArray = s.toCharArray();
    
    				int temp;
    				int low=0;
    				int high=stringArray.length-1;
    
    
    
    
    
    
    				while(low<high){
    					temp= stringArray[low];
    					stringArray[low] = stringArray[high];
    
    					System.out.print(stringArray[low]);
    
    
    					low ++;
    					high--;
    				}
    
    
    
    
    	}
    }
    If I enter in Hello Java as a string input I get this.

    avaj

    It's printing out one word reversed, but I need the whole sentence. How will I do this?

    Thank you.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You overcomplicated this a bit.

    Try prompting for input. Then start at the end of the string and loop downwards.

    Assuming you know how loops work

    a while loop accepts a condition and does something until it is false. if you want a counter, a for loop may be more helpful, however; it is possible with a while loop. Do you know any way to find the max length of a string?

    In your code try only decrementing high and see what happens.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    Then start at the end of the string and loop downwards.
    Exactly what I was suggesting in first reply. Great to see good advice getting ignored yet again.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    Exactly what I was suggesting in first reply. Great to see good advice getting ignored yet again.
    Haha, at least he is putting in some effort and trying to solve it instead of just continually prodding for more advice.

Similar Threads

  1. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 06:39 AM
  2. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 03:31 AM
  3. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 01:29 AM
  4. reverse a string with a while loop...
    By OptimusPrime in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 12:06 AM
  5. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •