patternjavaModerate
Reversing a domain name string in Java
Viewed 0 times
javanamereversingstringdomain
Problem
Given a dot separated domain name, reverse the domain elements.
Example:
I just wanted some suggestions to improve my code. Or if you can suggest a better way, please do so.
Example:
codereview.stackexchange.com -> com.stackexchange.codereview
google.com -> com.googleI just wanted some suggestions to improve my code. Or if you can suggest a better way, please do so.
package practice;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ReverseDomainName
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the domain name to be reversed");
String domain = sc.nextLine();
sc.close();
ReverseDomainName obj = new ReverseDomainName();
String reversedDomainName = obj.reverseDomainNameString(domain);
System.out.println("The reversed Domain Name = "+reversedDomainName);
}
public String reverseDomainNameString(String s)
{
String reverse1 = "",word ="" ;
int i;
StringTokenizer st = new StringTokenizer(s,".");
int len = st.countTokens();
/*
* The check is performed so that instead of com.stackexchange.codereview.
* we can get the proper com.stackexhange.codereview so no
* extra dot
*/
word = st.nextToken();
reverse1 = word+reverse1;
for(i=1;i<len;i++)
{
word = st.nextToken();
reverse1 = word +"."+reverse1;
}
return reverse1;
}
}Solution
StringTokenizer is an OK solution, but I think that String.split() would lead to a less verbose solution.Here is a Java 8 solution that uses
split():public static String reverseDomainNameString(String s) {
List components = Arrays.asList(s.split("\\."));
Collections.reverse(components);
return String.join(".", components.toArray(new String[0]));
}Without Java 8's
String.join(), though, the solution would be more tedious. Note that it might be a good idea to ensure that the string is non-empty, otherwise components[0] (or st.nextToken() in your original solution) would crash.public static String reverse(String s) {
if (s == null || s.isEmpty()) return s;
String[] components = s.split("\\.");
StringBuilder result = new StringBuilder(s.length());
for (int i = components.length - 1; i > 0; i--) {
result.append(components[i]).append(".");
}
return result.append(components[0]).toString();
}Code Snippets
public static String reverseDomainNameString(String s) {
List<String> components = Arrays.asList(s.split("\\."));
Collections.reverse(components);
return String.join(".", components.toArray(new String[0]));
}public static String reverse(String s) {
if (s == null || s.isEmpty()) return s;
String[] components = s.split("\\.");
StringBuilder result = new StringBuilder(s.length());
for (int i = components.length - 1; i > 0; i--) {
result.append(components[i]).append(".");
}
return result.append(components[0]).toString();
}Context
StackExchange Code Review Q#113033, answer score: 10
Revisions (0)
No revisions yet.