-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_strnbld.c
72 lines (64 loc) · 1.76 KB
/
test_strnbld.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
65
66
67
68
69
70
71
// test_strnbld.c - test string builder and verify the behavior by
// performing the same procedures with strncat
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "strbld.h"
int main(int argc, char *argv[]) {
int i;
char mydest[20];
char *ret;
int n;
char *strings[] = {"Say", " ", "hello", " ", "world"};
int numstrings = 5;
for(n = 2; n <= 6; n++) {
memset(mydest, '#', sizeof(mydest));
mydest[sizeof(mydest) - 1] = '\0';
for(i = 0; i < numstrings; i++) {
if(i == 0) {
ret = strnbld(mydest, strings[i], n);
}
else {
ret = strnbld(ret, strings[i], n);
}
}
printf("test_strnbld - using strnbld (%d): Final string = [%s]\n", n, mydest);
memset(mydest, '#', sizeof(mydest));
mydest[sizeof(mydest) - 1] = '\0';
for(i = numstrings - 1; i >= 0; i--) {
if(i == numstrings - 1) {
ret = strnbld(mydest, strings[i], n);
}
else {
ret = strnbld(ret, strings[i], n);
}
}
printf("test_strnbld - using strnbld (%d): Final string = [%s]\n", n, mydest);
}
for(n = 2; n <= 6; n++) {
memset(mydest, '#', sizeof(mydest));
mydest[0] = '\0';
mydest[sizeof(mydest) - 1] = '\0';
for(i = 0; i < numstrings; i++) {
if(i == 0) {
ret = strncat(mydest, strings[i], n);
}
else {
ret = strncat(mydest, strings[i], n);
}
}
printf("test_strnbld - using strncat (%d) for comparison: Final string = [%s]\n", n, mydest);
memset(mydest, '#', sizeof(mydest));
mydest[0] = '\0';
mydest[sizeof(mydest) - 1] = '\0';
for(i = numstrings - 1; i >= 0; i--) {
if(i == numstrings - 1) {
ret = strncat(mydest, strings[i], n);
}
else {
ret = strncat(mydest, strings[i], n);
}
}
printf("test_strnbld - using strncat (%d) for comparison: Final string = [%s]\n", n, mydest);
}
}