Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:11 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: recursive string reversal- reverse string
PostPosted: Tue Dec 02, 2008 1:30 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

If you want to to reverse a string . I will show you two ways the first one is using the reverse method in jdk ,it is powerful function and easy .

Code:
public class MyClass {
       public String reverse(String newstring) {
            if ((null == newstring) || (newstring.length() <= 1)) {
                return newstring;
            }
            return new StringBuffer(newstring).reverse().toString();
        }
}


Notice that is no need to reverse a string with length =1 .

another solution is using recursion ,yes recursion , the idea is basic ,i know many programmers don't like recursion , but it is easy if you just think in it for a minute . ,take a look to code .

Code:
public String reverse(String newstring) {
            if ((null == newstring) || (newstring.length()  <= 1)) {
                return newstring;
            }
            return reverse(newstring.substring(1)) + newstring.charAt(0);
        }


other solutions you may need it or think about ,

1- Do a swap in StringBuffer
Code:
  public String reverseusingswap(String newstring) {
            if ((null == newstring) || (newstring.length()  <= 1 )) {
                return newstring;
            }
            StringBuffer stringresult = new StringBuffer(newstring);
            for (int i = 0; i < (newstring.length() / 2); i++) {
                int swapIndex = newstring.length() - 1 - i;
                char swap =  stringresultcharAt(swapIndex);
                stringresult.setCharAt(swapIndex,  stringresult.charAt(i));
                 stringresult.setCharAt(i, swap);
            }
            return  stringresult.toString();
        }



2. Swap an array

Code:
public String reverse(String newstring) {
            if ((null == newstring) || (newstring.length() <= 1)) {
                return newstring;
            }
            char[] chars = newstring.toCharArray();
            int right = chars.length - 1;
            for (int left = 0; left < right; left++) {
                char swap = chars[left];
                chars[left] = chars[right];
                chars[right--] = swap;
            }
            return new String(chars);
        }


I hope it is useful for you :)

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 replacing each tab by the string “Tab”,
 Breaking the String into Words
 search for string in a cell array
 Trim string in JavaScript
 How to read string text in files
 add string to another, Lowercase ,Uppercase
 reverse ,length of strings as number of bytes
 search in a string and replace
 get number of words in a string
 Check empty string

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team