0

I had a programming interview where they asked me to write a method that takes a long string and an int as arguments, and the method should split the string at every interval of the number. I couldn't understand how to do this, so I thought I would ask here to see if anyone has any ideas.

Btw: Unfortunately I didn't get the job...

user16655
  • 1,901
  • 6
  • 36
  • 60
  • 1
    Do not be sad. Read and use the Java API as much as possible. This will help to solve easy tasks like this. – szpetip Feb 10 '15 at 18:43
  • Thanks for nice (and not so nice...) comments. I get that this is something I should know, but I just don't. Could anyone write the solution? – user16655 Feb 10 '15 at 18:48
  • 1
    A for loop and the String `substring` method is all that is needed. – David Conrad Feb 10 '15 at 18:53
  • 1
    See [http://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java](http://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java) for a similar question. – jHilscher Feb 10 '15 at 19:04
  • @jHilscher Thanks for pointing out the availability of `\\G` in java. – collapsar Feb 10 '15 at 19:25

2 Answers2

5

Sorry to hear about the interview. It sucks... and it happens. +1 for following up the question.

In the splitter function, I run through the "long string" with an index. Every iteration I use the String.substring method to extract that size of the interval from the string. (index + interval). After the extraction, I increment the index with the interval. Thus, moving through the long string, interval by interval.

It can happen that the index + interval is larger than the length of the long string. (will cause out of bound exception) Thus the extra check to avoid it, and save the remaining string.

public static void main(String[] args) {
    String veryLongString = "12345678901234567890";
    List<String> subStrings = splitter(veryLongString, 3);
    // Print the result
    for (String s : subStrings) {
        System.out.print(s + " ");
    }
}

public static List<String> splitter(String string, int interval) {
    int index = 0;
    List<String> subStrings = new ArrayList<String>();
    while (index < string.length()) {
        // Check if there is still enough characters to read.
        // If that is the case, remove it from the string.
        if (index + interval < string.length()) {
            subStrings.add(string.substring(index, index + interval));
        } else {
            // Else, less the "interval" amount of characters left,
            // Read the remaining characters.
            subStrings.add(string.substring(index, string.length()));
        }
        index += interval;
    }
    return subStrings;
}

Output:

123 456 789 012 345 678 90

Reg
  • 10,717
  • 6
  • 37
  • 54
  • 2
    Thank you so much. Seems completely obvious now, but I guess I still got some learning to do. I (of course) should have been able to solve this. Thanks again! :) – user16655 Feb 10 '15 at 19:23
1

Sorry for the misfortune. Better luck next time. I would like to answer this question using recursivity. It makes the algorithm simpler.

Here is the principle that I followed

  1. If the length of the string is less or equal to interval we store it and we get out of the method
  2. Otherwise we take the substring going from index 0 to interval -1, which is String.subString(0, interval) and we store then we call the method on String.subString(interval)

Here is the code:

import java.util.ArrayList;
import java.util.List;

public class StringHelper {

    private static List<String> workList = new ArrayList<String>();

    private StringHelper()
    {

    }

    public static void intervallSplit(String datas, int interval) {
        if (datas.length() <= interval) {
            workList.add(datas);
            return;
        }

        workList.add( datas.substring(0,interval));
        intervallSplit(datas.substring(interval), interval);


    }


    public static void main(String[] args) {
        String text = "1234567891011121314151617181920";
        intervallSplit(text, 3);

        System.out.println(workList);
    }

}

And here is the output from the example with sample datas

[123, 456, 789, 101, 112, 131, 415, 161, 718, 192, 0]

Reg
  • 10,717
  • 6
  • 37
  • 54
alainlompo
  • 4,414
  • 4
  • 32
  • 41