-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeArray.java
More file actions
31 lines (22 loc) · 762 Bytes
/
MergeArray.java
File metadata and controls
31 lines (22 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MergeArray {
public String mergeAlternately(String word1, String word2) {
StringBuilder Ans = new StringBuilder();
int i = 0;
while(i < word1.length() || i < word2.length()){
if(i < word1.length()){
Ans.append(word1.charAt(i));
}
if(i < word2.length()){
Ans.append(word2.charAt(i));
}
i++;
}
return Ans.toString();
}
public static void main(String[] args) {
MergeArray obj = new MergeArray();
String word1 = "abc";
String word2 = "pqr";
System.out.println("Merged String: " + obj.mergeAlternately(word1, word2));
}
}