-
Notifications
You must be signed in to change notification settings - Fork 24
/
morgan-and-a-string.c
61 lines (52 loc) · 1.22 KB
/
morgan-and-a-string.c
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
// Morgan and a String
// Find the lexicographically minimal string that can be formed by the combination of two strings.
//
// https://www.hackerrank.com/challenges/morgan-and-a-string/problem
//
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
char *morgan;
void morganAndString(const char *a, const char *b) {
// Complete this function
char *r = morgan;
while (*a && *b)
{
if (strcmp(a, b) < 0)
{
*r++ = *a++;
if (!*a) { strcpy(r, b); return; }
}
else
{
*r++ = *b++;
if (!*b) { strcpy(r, a); return; }
}
}
*r = 0;
}
int main() {
int t;
char *a, *b;
a = (char *)malloc(128000 * sizeof(char));
b = (char *)malloc(128000 * sizeof(char));
morgan = (char *)malloc(256000 * sizeof(char));
scanf("%i", &t);
for(int a0 = 0; a0 < t; a0++) {
scanf("%s", a);
scanf("%s", b);
strcat(a, "z");
strcat(b, "z");
morganAndString(a, b);
morgan[strlen(a) + strlen(b)-2] = 0;
printf("%s\n", morgan);
}
free(a);
free(b);
free(morgan);
return 0;
}