dns - Tracing a bug in a "reverse domain" program in Java -
my code supposed reverse domain. example, if input www.google.com.uk, should print uk.com.google.www
my problem code prints uk.com..google.www
i can't figure . comes or how remove it. did tracing , found www.stack.com.edu.uk return uk.edu..com..stack.www
import java.util.*; public class domainreverse { public static void main(string[] args) { scanner sc = new scanner(system.in); string domain = sc.nextline(); int x; int start = 0; string reversed_domain = ""; boolean flag = false; do{ x = domain.indexof(".",start+1); if(x==-1){ flag = true; break; } reversed_domain = domain.substring(start,x+1) +reversed_domain; start = x; } while(flag==false); int length = domain.length(); int z = domain.lastindexof("."); string enddomain = domain.substring(z+1,length); reversed_domain = enddomain+reversed_domain; int p = reversed_domain.lastindexof("."); reversed_domain = reversed_domain.substring(0,p); system.out.print(reversed_domain); } }
Comments
Post a Comment