-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeAlternateStringWords.java
More file actions
69 lines (59 loc) · 1.79 KB
/
MergeAlternateStringWords.java
File metadata and controls
69 lines (59 loc) · 1.79 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class MergeAlternateStringWords {
static void mergeAlternateString(String s1, String s2) {
// String Builder is mutable, but string is immutable
// Solve using Character iterator also works uncomment if you want
/*StringBuilder output = new StringBuilder();
CharacterIterator ch = new StringCharacterIterator(s1);
CharacterIterator ch1 = new StringCharacterIterator(s2);
//do while
char c= ch.first();
char c1= ch1.first();
do {
if (c != ch.DONE){
output.append(c);
}
if (c1 != ch1.DONE){
output.append(c1);
}
c = ch.next();
c1 = ch1.next();
}while ((c != ch.DONE) || (c1 != ch1.DONE));
System.out.print(output);
}*/
//Normal String traverse convert String to Char Array
StringBuilder output = new StringBuilder();
char c[] = s1.toCharArray();
char c1[] = s2.toCharArray();
int i=0;
int j=0;
// for loop does not solve my problem
/*for (i=0; i<c.length;i++){
output.append(c[i]);
if(i>j )
{
j++;
}
for (;j<c1.length && j<=i ;j++){
output.append(c1[j]);
}
}*/
do
{
if(i<c.length)
output.append(c[i]);
if(j<c1.length)
output.append(c1[j]);
i++;
j++;
}while (i<c.length || j<c1.length);
System.out.print(output);
// convert String builder to String.
output.toString();
}
public static void main(String[] args) {
System.out.println("Hello world!");
String str = "abc";
String str2 = "efghi";
mergeAlternateString(str,str2);
}
}