Skip to content

Commit e819b93

Browse files
committed
String Merge
1 parent 85cf1f7 commit e819b93

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/easy/StringMerge.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package easy;
2+
3+
/**
4+
* Have the function StringMerge(str) read the str parameter being passed
5+
* which will contain a large string of alphanumeric characters with
6+
* a single asterisk character splitting the string evenly into two separate strings.
7+
* Your goal is to return a new string by pairing up the characters
8+
* in the corresponding locations in both strings.
9+
* For example: if str is "abc1*kyoo" then your program should return the string akbyco1o
10+
* because a pairs with k, b pairs with y, etc.
11+
* The string will always split evenly with the asterisk in the center.
12+
*/
13+
public class StringMerge {
14+
15+
/**
16+
* String Merge function.
17+
*
18+
* @param str input string
19+
* @return a new string with paired up characters
20+
*/
21+
public static String stringMerge(String str) {
22+
StringBuilder output = new StringBuilder();
23+
String[] strArr = str.trim().split("\\*");
24+
String str1 = strArr[0];
25+
String str2 = strArr[1];
26+
for (int i = 0; i < str1.length(); i++) {
27+
output.append(str1.charAt(i)).append(str2.charAt(i));
28+
}
29+
return output.toString();
30+
}
31+
32+
/**
33+
* Entry point.
34+
*
35+
* @param args command line arguments
36+
*/
37+
public static void main(String[] args) {
38+
String result = stringMerge("123hg*aaabb");
39+
System.out.println(result);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)