Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: how to reverse string without using string reverse or array ?

  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default how to reverse string without using string reverse or array ?

    hi guys ,

    Can any one help me in reversing a string without using predefined string reverse function or with array ?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to reverse string without using string reverse or array ?

    Your question is unclear, are you saying you are also prohibited from using an array?

    Thats fine if so, because you don't need one. Do you know what string concatenation is, and how to use a 'for' loop?

  3. #3
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default Re: how to reverse string without using string reverse or array ?

    Quote Originally Posted by quad64bit View Post
    Your question is unclear, are you saying you are also prohibited from using an array?

    Thats fine if so, because you don't need one. Do you know what string concatenation is, and how to use a 'for' loop?
    Thanks for you reply ,

    Yes i am prohibited from using an array . yes i know string concatenation is combing two strings , but i haven t worked much on string concatenation , but how string concat() helps here ? . yes of course i know to use for loop .

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to reverse string without using string reverse or array ?

    "yes of course i know to use for loop"

    Ok, great! Then the problem should be trivial. You are also aware that java does string concatenation implicitly as well, such as:
    Java Code:
    String alpha = "a";
    String beta = "b";
    String combo = alpha + beta;
    assert combo.equals("ab") == true;
    You now have the tool needed to combine any two strings. We also know that loops let us do tasks many times over, say, for every character in a 100 character string. If I wanted to make the above example read 'ba' instead of 'ab' (reversed), then I need only change the order in which I concat them.

    Java Code:
    String alpha = "a";
    String beta = "b";
    String combo = beta + alpha;
    assert combo.equals("ba") == true;
    Now, one last part. We can do this with whole strings, but what about individual characters? Well, take a look at the charAt() method of the string class. Combine this with a loop, a temporary String variable, implicit concatenation, and implicit casting from char to String, and you have your solution. Give it a try and post what you come up with; we can discuss it more!

  5. #5
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default Re: how to reverse string without using string reverse or array ?

    Quote Originally Posted by quad64bit View Post
    "yes of course i know to use for loop"

    Ok, great! Then the problem should be trivial. You are also aware that java does string concatenation implicitly as well, such as:
    Java Code:
    String alpha = "a";
    String beta = "b";
    String combo = alpha + beta;
    assert combo.equals("ab") == true;
    You now have the tool needed to combine any two strings. We also know that loops let us do tasks many times over, say, for every character in a 100 character string. If I wanted to make the above example read 'ba' instead of 'ab' (reversed), then I need only change the order in which I concat them.

    Java Code:
    String alpha = "a";
    String beta = "b";
    String combo = beta + alpha;
    assert combo.equals("ba") == true;
    Now, one last part. We can do this with whole strings, but what about individual characters? Well, take a look at the charAt() method of the string class. Combine this with a loop, a temporary String variable, implicit concatenation, and implicit casting from char to String, and you have your solution. Give it a try and post what you come up with; we can discuss it more!

    Wooooooooooooow you have amazing skill quad64bit . Thanks a lot for a wonderful tips . I will try this and i will post here , i am a old member but still i was away from java so again i am starting , any way thanks a lot

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to reverse string without using string reverse or array ?

    No problem. Feel free to post what you come up with, and I'll gladly help you further if you need it!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to reverse string without using string reverse or array ?

    Think recursion:

    1) an empty String has itself as its reverse;
    2) otherwise a String s can be written as ht where h is a single character and t is the original String s with its first character stripped; the reverse of this String ht is the reverse of t with character h appended.

    kind regards,

    Jos
    Last edited by JosAH; 03-09-2012 at 07:43 AM.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: how to reverse string without using string reverse or array ?

    Quote Originally Posted by quad64bit View Post
    Java Code:
    assert combo.equals("ab") == true;
    You know I'm going to have to pull you up on this...:)
    Java Code:
    assert combo.equals("ab");
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to reverse string without using string reverse or array ?

    Yeah yeah, I know :D

    I was trying to make it painfully obvious what was happening. I'm never that explicit in my integration tests, I just didn't want any confusion for someone who's skill level isn't known :)

  10. #10
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default Re: how to reverse string without using string reverse or array ?

    Quote Originally Posted by quad64bit View Post
    No problem. Feel free to post what you come up with, and I'll gladly help you further if you need it!
    Hi quad64bit , sorry for my late reply , here this is what i tried , but i slightly copied from some resource , but the one thing i wanted to know , that is casting


    Java Code:
     I TIRED WITH THE HELP OF OTHER RESOURCE 
             ---------------------------------------------
    public class ReverseString{
    public static void main(String args[])
    {
    	
    	String s = "Helloworld";
    	
    	
    	StringBuilder sb = new StringBuilder();
    	   for(int i = s.length() - 1; i >= 0; --i)
    	     sb.append(s.charAt(i));
    	   System.out.println(sb.toString());
    		 
    	}
    }
    Java Code:
    I TRIED ON YOUR OWN USING YOUR IDEA 
             ----------------------------------------
    public class ReverseString{
    public static void main(String args[])
    {
    	
    	String s = "Helloworld";
    for(int i = s.length() - 1; i >= 0; --i)
    {
    	     s.charAt(i);
    	   
    	     System.out.print((char)i);
    }
    }
    }
    Here the above second one is mine and it is not giving right answer , here i don't how to cast the int to string :( . please help me bro . And what is "assert" ?what is the use of "assert " in our program

    Quote Originally Posted by JosAH View Post
    Think recursion:

    1) an empty String has itself as its reverse;
    2) otherwise a String s can be written as ht where h is a single character and t is the original String s with its first character stripped; the reverse of this String ht is the reverse of t with character h appended.

    kind regards,

    Jos
    Thank you very much for your reply Josh , sorry for the late reply ,

    Can you please explain me in detail of the two point which you have given above ? . thank you Josh

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to reverse string without using string reverse or array ?

    Quote Originally Posted by funkygarzon View Post
    Thank you very much for your reply Josh , sorry for the late reply ,

    Can you please explain me in detail of the two point which you have given above ? . thank you Josh
    There isn't much more detail to supply; if a String is empty, the reversed of the String is the empty String; otherwise reverse the String minus the first character and append the first character to the reverse of the String. One more step would be spoonfeeding; you do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. reverse string
    By mallikanala in forum New To Java
    Replies: 1
    Last Post: 01-21-2012, 03:40 PM
  2. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  3. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 05:39 AM
  4. How to reverse a string using while loops?
    By JavaS in forum New To Java
    Replies: 6
    Last Post: 03-08-2011, 03:01 AM
  5. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 02:31 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
  •