-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCString.cpp
More file actions
203 lines (167 loc) · 5.6 KB
/
Copy pathCString.cpp
File metadata and controls
203 lines (167 loc) · 5.6 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <string.h>
#include <iostream>
namespace {
namespace InitializeString {
void run() {
// Memory layout (addresses are illustrative):
// Stack (modifiable array)
// 0x7fffc000: str2[0] = 'H'
// 0x7fffc001: str2[1] = 'e'
// 0x7fffc002: str2[2] = 'l'
// 0x7fffc003: str2[3] = 'l'
// 0x7fffc004: str2[4] = 'o'
// 0x7fffc005: str2[5] = '\0'
// (str2 starts at 0x7fffc000)
// Data / Read-only segment (string literal)
// 0x00403000: 'H'
// 0x00403001: 'e'
// 0x00403002: 'l'
// 0x00403003: 'l'
// 0x00403004: 'o'
// 0x00403005: '\0'
// (str1 -> points to 0x00403000)
// Pointer variable
// 0x7fffbff0: str1 = 0x00403000 // str1 holds address of string literal
// 1. As a character array (modifiable)
char strArray[] = "this is a strArray literal";
std::cout << strArray << " - size " << sizeof(strArray) << " - length "
<< strlen(strArray) << "\n";
strArray[0] ^= ' ';
std::cout << strArray << "\n";
// 2. As a a pointer to a string literal const (read-only)
char* strPtr = "this is a strPtr literal";
std::cout << strPtr << " - size " << sizeof(strPtr) << " - length "
<< strlen(strPtr) << "\n";
// strPtr[0] ^= ' '; // ERROR
// 3. Using sprintf / snprintf (string formatting)
char strFormatted[50];
int numVar = 21;
// sprintf (unsafe if buffer too small)
sprintf(strFormatted, "sprintf: %d", numVar);
std::cout << strFormatted << " - size " << sizeof(strFormatted)
<< " - length " << strlen(strFormatted) << "\n";
// snprintf (safer, limits buffer size)
snprintf(strFormatted, sizeof(strFormatted), "snprintf %s %d", strArray,
numVar);
std::cout << strFormatted << " - size " << sizeof(strFormatted)
<< " - length " << strlen(strFormatted) << "\n";
}
} // namespace InitializeString
namespace CopyString {
void run() {
const char src[] = "CopyStr";
char dst[50];
// 1. Copy full string
strcpy(dst, src);
std::cout << dst << " - size " << sizeof(dst) << " - length " << strlen(dst)
<< "\n";
// 2. Copy the number of charactor
strncpy(dst, "Hello123", 5);
dst[5] = '\0'; // ensure null-termination
std::cout << dst << " - size " << sizeof(dst) << " - length " << strlen(dst)
<< "\n";
}
} // namespace CopyString
namespace ConcatString {
void run() {
const char part1[] = "Hello";
const char part2[] = "World";
char dst[50] = "";
// 1. Append full str
strcat(dst, part1);
strcat(dst, part2);
strcat(dst, " !!");
std::cout << dst << " - size " << sizeof(dst) << " - length " << strlen(dst)
<< "\n";
// 2. Append the number of charactors
strncat(dst, "1234", 3);
std::cout << dst << " - size " << sizeof(dst) << " - length " << strlen(dst)
<< "\n";
}
} // namespace ConcatString
namespace CompareString {
void run() {
const char str1[] = "abc";
const char str2[] = "abcde";
// 1. Compare full str
int result1 = strcmp(str1, "abc");
int result2 = strcmp(str1, str2);
std::cout << "strcmp(str1, \"abc\") = " << result1 << "\n";
std::cout << "strcmp(str1, str2) = " << result2 << "\n";
// 2. Compare first N characters
int result3 = strncmp(str1, str2, 3);
std::cout << "strncmp(str1, str2, 3) = " << result3 << "\n";
}
} // namespace CompareString
namespace ParseString {
void run() {
char str[] = "A,B,C,D,"; // OK
// char* str = "A,B,C,D,"; // ERROR - const
// 1. Splitting a string by some delimiter
// 1.1. strtok - not safe
char* delimiter = ",";
const char* token = strtok(str, delimiter);
while (token != nullptr) {
std::cout << "strtok - token :" << token << "\n";
token = strtok(NULL, delimiter);
}
{
std::cout << " === problem === \n";
char str1[] = "a,b,c";
char str2[] = "1,2,3";
// Parse str1
const char* token1 = strtok(str1, delimiter); // token1 = "a"
std::cout << "str1 first token: " << token1 << "\n";
// Parse str2
const char* token2 = strtok(str2, delimiter); // token2 = "1"
std::cout << "str2 first token: " << token2 << "\n";
// Continue parsing str1
token1 = strtok(nullptr, delimiter); // Unexpected!
std::cout << "str1 second token: " << token1 << "\n";
}
// Have to Reset string for the next handle because strtok
char str2[] = "one,two,three";
// 1.2. strtok_r - safe
char* saveptr;
const char* token2 = strtok_r(str2, delimiter, &saveptr);
while (token2 != nullptr) {
std::cout << "strtok - token :" << token2 << "\n";
token2 = strtok_r(NULL, delimiter, &saveptr);
}
// 2. strcspn: find first occurrence of any chars in reject set
const char sample[] = "hello123world";
size_t pos = strcspn(sample, "0123456789"); // pos = 5
std::cout << "Sample string: " << sample << "\n";
std::cout << "First digit from \"0123456789\" found at index: " << pos
<< "\n";
std::cout << "The digit is: " << sample[pos] << "\n";
}
} // namespace ParseString
namespace NumberConversion {
void run() {
// 1. string to integer
const char strNum[] = "100";
int num = atoi(strNum);
std::cout << num << "\n";
// 2. string to double
const char strNumD[] = "100.1234__123";
double numD = atof(strNumD);
std::cout << numD << "\n";
char* end;
numD = strtod(strNumD, &end);
std::cout << numD << " end part:" << end << "\n";
}
} // namespace NumberConversion
} // namespace
struct CStringAutoRunner {
CStringAutoRunner() {
std::cout << "\n--- C String Example ---\n";
InitializeString::run();
CopyString::run();
ConcatString::run();
CompareString::run();
ParseString::run();
NumberConversion::run();
}
};
static CStringAutoRunner instance;