Friday, 6 February 2015

Write a program to expand a given string x to y?

input string x = "1..5,8,11..14,18,20,26..29" output string y = "1,2,3,4,5,8,11,12,13,14,18,20,26,27,28,29" import java.util.*; class StringDemo1 { public static void main(String[] args) { String x="1..5,8,11..14,18,20,26..29"; String sarr[]=x.split(","); StringBuilder sb=new StringBuilder(); for (String s:sarr) { if(s.indexOf('.')!=-1) { String limits[]=s.split("\\.\\."); int llimit=Integer.parseInt(limits[0]); int ulimit=Integer.parseInt(limits[1]); for (int i=llimit;i<=ulimit;i++) { sb.append(""+i+","); } } else sb.append(s+","); } System.out.println(sb); } }

No comments:

Post a Comment