Reputation: 31
For context, the method needs to insert dashes into a string in a 1, 2, 4, 1, 2, 4... pattern. For example, a string that holds "Overflow" would be output as "O-ve-rflo-w". Would I use nested for loops in this situation?
Upvotes: 1
Views: 293
Reputation: 1
Method:
private static String addDashes(String string, int... pattern) {
String output = "";
int index = 0;
while (true)
for (int p : pattern) {
if (index + p >= string.length())
return output += string.substring(index);
output += string.substring(index, index += p) + "-";
}
}
Call Method:
System.out.println(addDashes("Overflow", 1,2,4));
Output:
O-ve-rflo-w
Upvotes: 0
Reputation: 6307
The other answer using a pattern is a great solution, however, you could also use a recursive method. This may not be a compact solution, but the logic is easy to follow:
//Process the string in chunks of 7 characters
public static String addFormatting(String input){
String formatted = "";
//Add first character
if(input.length() >= 1) formatted = input.substring(0, 1);
//Add dash and the next 2 characters, else the remainder of the string
if(input.length() >= 3) formatted += "-" + input.substring(1,3);
else if (input.length() > 1) formatted += "-" + input.substring(1);
//Add dash and the next 4 characters, else the remainder of the string
if(input.length() >= 7) formatted += "-" + input.substring(3,7);
else if (input.length() > 3) formatted += "-" + input.substring(3);
//Add dash and recursivly format the next chunk
if(input.length() > 7){
formatted += "-";
return formatted + addFormatting(input.substring(7));
}
//else return the complete formatted once it has been fully processed
else return formatted;
}
To call the method simply use addFormatting("OverflowisagreatQnAsite!");
the printed output would O-ve-rflo-w-is-agre-a-tQ-nAsi-t-e!
Upvotes: 2
Reputation: 2776
You can do the following:
private static String applyPattern(List<Integer> pattern, String str) {
int currentPatternIndex = 0;
int iterationsTillNextDash = pattern.get(currentPatternIndex);
StringBuilder stringBuilder = new StringBuilder();
for (char aChar : str.toCharArray()) {
if (iterationsTillNextDash == 0) {
stringBuilder.append('-');
iterationsTillNextDash = pattern.get(++currentPatternIndex % pattern.size());
}
iterationsTillNextDash--;
stringBuilder.append(aChar);
}
return stringBuilder.toString();
}
Usage:
String strWithDashes = applyPattern(Arrays.asList(1, 2, 4), "Overflow");
System.out.println(strWithDashes);
Output:
O-ve-rflo-w
Upvotes: 2
Reputation: 186
Here is a simple hard-coded example for your situation. Perhaps you can figure out a way to use modulus %
in your code for words longer than "overflow".
class Main {
public static String addDashes(String s)
{
String s_with_dashes = "";
for(int i = 0; i < s.length(); i++)
{
if(i == 1 || i == 3 || i == 7)
{
s_with_dashes += '-';
}
s_with_dashes += s.charAt(i);
}
return s_with_dashes;
}
public static void main(String[] args)
{
String s = "Overflow";
String s_with_dashes = addDashes(s);
System.out.println(s_with_dashes);
}
}
Upvotes: 0