-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExpand_a_string.cpp
46 lines (42 loc) · 937 Bytes
/
Expand_a_string.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 1000
// Put the final string after expansion in 'res'
void expandString(char *str, char *res) {
// Write your code here
int n = strlen(str);
int i = 0, j = 0;
for (i = 0; i < n; i++) {
char c = str[i];
int count = 1;
char temp[50];
int k = 0;
while (i + 1 < n && str[i + 1] >= '0' && str[i + 1] <= '9') {
temp[k] = str[i + 1];
k++;
i++;
}
temp[k] = '\0';
if (strlen(temp) > 0)
count = atoi(temp);
while (count--)
{
res[j] = c;
j++;
}
}
res[j] = '\0';
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
char str[100];
scanf("%s", str);
char res[MAX_LEN];
expandString(str, res);
printf("%s\n", res);
}
return 0;
}