forked from thesstefan/The-C-Programming-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-5.c
More file actions
59 lines (40 loc) · 1.27 KB
/
5-5.c
File metadata and controls
59 lines (40 loc) · 1.27 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
#include <stdio.h>
#include <string.h>
void string_copy(char copy[], char target[], int length) {
for ( ; *copy != '\0' && length; length--)
*target++ = *copy++;
*target = '\0';
}
void string_concanetation(char result[], char to_add[], int length) {
int index = strlen(result);
for ( ; *to_add != '\0' && length; index++, length--)
*(result + index) = *to_add++;
*(result + index) = '\0';
}
int string_compare(char first[], char second[], int length) {
int full_length = 0;
int index = 0;
if (length == strlen(second))
full_length = 1;
for ( ; *(first + index) != '\0' && length; index++, length--) {
if (*(second + index) == '\0')
return 1;
if (*(first + index) < *(second + index))
return -1;
if (*(first + index) > *(second + index))
return 1;
}
if (*(second + index) != '\0' && full_length)
return -1;
return 0;
}
int main() {
char string_1[100] = "First String";
char string_2[100] = "Second String";
printf("%d\n", string_compare(string_1, string_2, 12));
string_concanetation(string_1, string_2, 4);
printf("%s\n", string_1);
string_copy(string_1, string_2, 4);
printf("%s\n", string_2);
return 0;
}