Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ private AlternativeStringArrange() {

/**
* Arranges two strings by alternating their characters.
* If one string is longer than the other, the remaining characters of the longer string
* are appended at the end of the result.
*
* @param firstString the first input string
* @param secondString the second input string
* @param firstString the first input string, must not be {@code null}
* @param secondString the second input string, must not be {@code null}
* @return a new string with characters from both strings arranged alternately
* @throws IllegalArgumentException if {@code firstString} or {@code secondString} is {@code null}
*/
public static String arrange(String firstString, String secondString) {
if (firstString == null || secondString == null) {
throw new IllegalArgumentException("Input strings must not be null");
}

StringBuilder result = new StringBuilder();
int length1 = firstString.length();
int length2 = secondString.length();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class AlternativeStringArrangeTest {
Expand All @@ -20,4 +22,15 @@ private static Stream<Object[]> provideTestData() {
void arrangeTest(String input1, String input2, String expected) {
assertEquals(expected, AlternativeStringArrange.arrange(input1, input2));
}

@ParameterizedTest(name = "null input ({0}, {1}) should throw IllegalArgumentException")
@MethodSource("provideNullInputs")
void arrangeThrowsOnNullInput(String input1, String input2) {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> AlternativeStringArrange.arrange(input1, input2));
assertEquals("Input strings must not be null", ex.getMessage());
}

private static Stream<Arguments> provideNullInputs() {
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
}
}
Loading