-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_strapis.cc
65 lines (50 loc) · 1.15 KB
/
test_strapis.cc
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
#include <iostream>
#include <cstring>
#include <cassert>
//#include <cstdlib>
using namespace std;
/*
size_t myStrLen(const char* str)
{
size_t sz = 0;
if (!str)
return sz;
const char* ptr = str;
while (*ptr != '\0') {
sz++;
ptr++;
}
return sz;
}
*/
size_t myStrLen(const char* str)
{
const char *ptr;
for (ptr = str; *ptr; ++ptr) {}
// if (!(*ptr))
// cout << "NULL: " << *ptr << endl;
return (ptr - str);
}
// Return destination string
char* myStrCpy(char* to, const char* from)
{
assert(to != NULL && from != NULL);
char* ptrTo = to;
while((*ptrTo++ = *from++) != '\0');
return to;
}
int main(int argc, char** argv)
{
const char* tests[] = {"", "a", "allen", "allen chin!"};
for (int i = 0; i < 4; i++)
{
cout << tests[i] << ": " << myStrLen(tests[i]) << ", " << strlen(tests[i]) << "(truth)" << endl;
}
char test_out1[14];
char test_out2[1];
char* ret = myStrCpy(test_out1, tests[2]);
cout << "Result1: " << ret << endl;
ret = myStrCpy(test_out2, tests[2]);
cout << "Result2: " << ret << endl;
return 0;
}