-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerStrAlt.java
More file actions
40 lines (38 loc) · 1.13 KB
/
merStrAlt.java
File metadata and controls
40 lines (38 loc) · 1.13 KB
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
32
33
34
35
36
37
38
39
40
// String word="";
// int a=0,b=0;
// int length=word1.length()+word2.length();
// for (int i = 0; i < length; i++) {
// if (i%2==0) {
// word=word+word1.charAt(a);
// // System.out.println(a);
// a++;
// }
// else {
// word=word+word2.charAt(b);
// // System.out.println(b);
// b++;
// }
// }
// return word;
public class merStrAlt {
public static String mergeAlternately(String word1, String word2) {
StringBuilder s=new StringBuilder();
int n1=word1.length();
int n2=word2.length();
int i=0;
while (i<n1||i<n2) {
if (i<n1) {
s.append(word1.charAt(i));
}
if (i<n2) {
s.append(word2.charAt(i));
}
i++;
}
return s.toString();
}
public static void main(String[] args) {
String str=mergeAlternately("ab", "pqrs");
System.out.println(str);
}
}