Results 1 to 10 of 10
  1. #1
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default reverse a string with a while loop...

    i found it tricky, i need to reverse a string with a while loop, no recursions allowed, and i should do it by cutting the first letter, put it as a last one than the 2nd letter etc...
    i tried with all my power and i still get the same string...
    here's the code:
    public static String reverseIterative(String s) {
    int count = 0;
    int r = s.length();
    while (count < r) {
    s = s.substring(1) + s.charAt(0);
    count++;
    }
    return s;
    }
    any help?
    thanks
    OP

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Remove first letter and append it to rear of string:
    Java Code:
    swap
    waps
    apsw
    pswa
    swap
    Of course.
    How else might we attempt this?

  3. #3
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    i need the reversed word which means: paws
    and you can append only with stringbuilder, i wanna use string.
    thanks for trying :)

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    I believe hardwired was just showing you what your current method was doing...

    Instead of using the same String object try creating a new one and appending the letter to the beginning of it

    Example in somewhat a pseudocode
    Java Code:
    String newString
    
    Loop through the main word:
         newString = 1st Character + newString
         Remove 1st Character from main word
    EndLoop
    
    Return newString

  5. #5
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    ok,
    i know what to do its just a stupid index game...
    ill manage i guess..
    thanks

  6. #6
    gcampton Guest

    Default

    you have the advantage of knowing the first letter (0), and the last letter (string.length)
    use this logic and rewrite your code:

    for the length of string / 2 (use a for loop, and the .length cmd)
    swap the last char with the first, increment firstcount, decrement lastcount.
    Last edited by gcampton; 12-28-2009 at 10:53 PM.

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Of course.
    How else might we attempt this?
    Be openminded ;-)

    Java Code:
    		String myWord = "asdfjkl";
    		char[] myWordA = myWord.toCharArray();
    		for (int i = 0; i < myWordA.length/2; i++) {
    			char swap = myWordA[i];
    			myWordA[i] = myWordA[myWordA.length - 1 - i];
    			myWordA[myWordA.length - 1 -i] = swap;
    		}
    		System.out.println(new String(myWordA));
    :cool:
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    doh'

    gcampton was first :P
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  9. #9
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    thank you all, but i need to use a while loop, and i managed to do this:

    public static String reverseIterative(String s) {
    int sIndex=1;
    int sLength = s.length();
    String newS = s.substring(0,1);
    while (sIndex < sLength){
    newS = s.substring(sIndex, sIndex + 1) + newS;
    sIndex++;
    }
    return newS;
    }

    its working also...
    took me like 2 hours...

  10. #10
    gcampton Guest

    Default

    try
    Java Code:
    int lastlettercount, firstlettercount=0, strLength=0;
    while (strLength< ( myWord.length / 2 ))
    {
       strLength++;
       
       // now swap first letter with last letter using substring. and temp.
       string temp;
       lastLetterCount = myWord.length;
       ....myWord.substring(firstLetterCount++, lastLetterCount--);
    }

Similar Threads

  1. Replies: 0
    Last Post: 12-11-2009, 10:26 PM
  2. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  3. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  4. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  5. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 PM

Posting Permissions

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