share

Thursday, 19 March 2015

Expand String


 Write A Function

String expnadString(String x);

string x="1,2,3,6";

output : 1,2,3,4,5,6

x=1,....,4....,10

output:

1,2,3,4,5,6,7,8,9,10


public class ExpandMyString {

    public static void main(String[] args) {
        TestData obj = new TestData();
        String x = "1,2,3,15,......,20,....,30";
        System.out.println("out put=" + obj.expnadString(x));
    }

    public String expnadString(String x) {
        String[] namesOfArray = x.split(",");
        char[] arr = namesOfArray[namesOfArray.length - 1].toCharArray();
        int i = 0;
        int num = 0;
        int zeroAscii = (int) '0';
        while (i < arr.length) {
            int charAscii = (int) arr[i];
            num = num * 10 + (charAscii - zeroAscii);
            i++;
        }

        String y = "";
        for (int j = 1; j < num; j++) {
            y += j + ",";
        }
        y += num;

        return y;
    }

}






 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).

No comments:

Post a Comment