-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathurl_formatter.c
64 lines (60 loc) · 1.7 KB
/
url_formatter.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
62
63
64
/*
* Date: 2018-06-18
*
* Description:
* Update input string to replace space(" ") with "%20".
*
* Approach:
* Effectively one character is being replaced with 3 characters so first we can
* count the number of spaces in input string and find the effective length of
* new string.
* Also, start from last so that overwriting input string is not an issue.
*
* Complexity:
* Time - O(n), Space - O(1)
*
* Note:
* As character array is mutable (can be updated in-place) in C so we can update
* input string(new string is not required). In other languages like python
* where string is immutable we will have to create a new string and can be done
* in one traversal.
*/
#include "stdio.h"
#include "string.h"
/*
* Updates input string (in-place) to have space replaced with %20.
* Args:
* s: Input string.
*
* Returns updated string.
*/
char *url_foramtter(char s[]) {
unsigned int new_length = 0, original_length = strlen(s);
unsigned int i = 0, new_idx = 0, space_count = 0;
for (i = 0; i < original_length; i++) {
if (s[i] == ' ')
space_count++;
}
new_length = original_length + 2 * space_count;
printf("Length of original string: %d\n", original_length);
printf("Spaces in original string: %d\n", space_count);
printf("Length of updated string: %d\n", new_length);
new_idx = new_length;
for (i = original_length; i > 0; i--) {
if (s[i - 1] == ' ') {
s[--new_idx] = '0';
s[--new_idx] = '2';
s[--new_idx] = '%';
} else {
s[--new_idx] = s[i - 1];
}
}
return s;
}
int main() {
char input[100] = {'\0'};
printf("Enter input string: ");
fgets(input, 100, stdin);
printf("\nUpdated string: %s\n", url_foramtter(input));
return 0;
}