-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
executable file
·104 lines (77 loc) · 2.38 KB
/
string.cpp
File metadata and controls
executable file
·104 lines (77 loc) · 2.38 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
// Note: part of string funcs are defined in "tools/files.cpp"
#include <string>
#include <vector>
#include <sstream> // for "std::stringstream"
#include <iostream> // for "std::cout"
#define print(x) std::cout << x << std::endl
bool is_number(std::string &s) {
return s.find_first_not_of("0123456789") == std::string::npos;
}
std::string strConcat(std::vector<std::string> strVec)
{
std::stringstream stream;
for (auto s: strVec) {
stream << s;
}
return stream.str();
}
std::string zeroFmt(int num, int n)
{
// input: zeroFmt(4, 23); output: "0023"
std::string s = std::to_string(n);
if (num > s.length()) {
return std::string(num - s.length(), '0') + s;
} else {
return std::to_string(n);
}
}
int strFind()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found != std::string::npos)
print("first 'needle' found at: " << found);
found = str.find("needles are small", found + 1, 6);
if (found!=std::string::npos)
print("second 'needle' found at: " << found);
found = str.find("haystack");
if (found != std::string::npos)
print("'haystack' also found at: " << found);
found = str.find('.');
if (found != std::string::npos)
print("Period found at: " << found);
// let's replace the first needle:
str.replace(str.find(str2), str2.length(), "preposition");
std::cout << str << '\n';
return 0;
}
int main()
{
const char *e = "char";
std::string s = "string";
print(s + e); // can add them together
char buffer[100];
int age = 23;
sprintf(buffer, "my age is %d", age);
print(buffer);
std::string sbuffer(buffer);
std::string sbuffer1 = "my age is 23";
print(sbuffer.compare(sbuffer1)); // 0: equal / other number: not equal
bool aaa = true;
bool bbb = false;
bool ccc = !bbb;
print(!aaa); // 0 false
print(!bbb); // 1 true
print(!ccc << "\n---"); // 0 false
int xxx = 1;
int yyy = 0;
print(!xxx);
// Concate the string list altogether
// std::vector<std::string> sv = {"1920", "x", "1080"};
// std::string s1 = strConcat({1920, "x", 1080});
// print(s1.c_str());
// strFind();
return 0;
}