Solved

Need to reverse a string in Java

Posted on 2008-10-20
9
602 Views
Last Modified: 2012-05-05
Hi Folks,

I need an easy way to reverse a string in java, but not character by character, but separated by a fullstop.

For example .. I need

127.0.0.2
to be reversed to
2.0.0.127

Another example

192.168.0.1
to be reversed to
1.0.168.192

Any help would be much appreciated.

Cheers.
0
Comment
Question by:JigsawTech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 22756462
Easiest:  split into numbers,
reverse the numbers,
rebuild the string.

;JOOP!
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22756464
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
ID: 22756485
String original = "127.0.0.2";

String[] k = original.split("\\.");

String new = k[3] + '.' + k[2] + '.' + k[1] + '.' + k[0];

;JOOP!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:grahamcorp
ID: 22756589
I would imagine you could break this into smaller substrings based on the '.'?
So for example,
Sorry if the syntax etc isn't right, but hopefully you get the idea?
May not be most efficient solution.

String ip = '192.168.0.1';
 
String [] tempIP = ip.split('.');
 
public static string reverseJoin( String[] ip, String delimiter)
{
   ip = reverseArray(ip);
 
   StringBuffer buffer = new StringBuffer();
   Iterator iter = ip.iterator();
 
   while(iter.hasNext())
   {
      buffer.append(iter.next());
      if( iter.hasNext() )
      {
          buffer.append(delimiter)
      }
   }
   return buffer.toString();
}
 
private static reverseArray(String [] arrayToReverse)
{
   String [] tempArray;
 
   for( int i = 0; i < tempIP.length; i++ )
   {
      tempArray[i] = tempIP[tempIP.length-i];
   }
   
   return tempArray;
}

Open in new window

0
 
LVL 1

Expert Comment

by:grahamcorp
ID: 22756601
I wasn't sure if the ip thing was just an example, i.e. is the string also going to be in the format <num>.<num>.<num>.<num>?
If it is then sciuriware is a better solution than mine as mine will be overkill.
0
 
LVL 9

Expert Comment

by:darkapple
ID: 22756685
Ok here is a function i've created to reverse string as per your requirement.
	private String strRev(String str){
		
		String rev = "";
 
		String[] k = str.split("\\.");
		
 
		for(int i=k.length; i!=0; i--){
			
			rev = rev.concat(k[i-1]);
			rev = ((i-1)>0?rev.concat("."):rev.concat(""));
		}
 
		return rev;
	}

Open in new window

0
 
LVL 1

Author Closing Comment

by:JigsawTech
ID: 31507777
Thanks alot, works great.
0
 
LVL 1

Author Comment

by:JigsawTech
ID: 22756745
hi folks,

Appreciate the speedy responses .. didn't even think about using split..  doh.

Thanks again.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22756951
Have a good time with avaJ.

!POOJ;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 74
Is there a simpler dropbox system? 10 44
dao vs facade design patterns 2 31
batch vs regular insert in spring DAO 2 21
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question