-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrimString
More file actions
135 lines (122 loc) · 2.52 KB
/
TrimString
File metadata and controls
135 lines (122 loc) · 2.52 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
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//字符串转大写
void stringToHight(string& strTxt)
{
for (int i = 0; i < strTxt.length(); i++)
{
if (strTxt.at(i) >= 0x61 && strTxt.at(i) <= 0x7A)
{
strTxt.at(i) -= 0x20;
}
}
}
//字符串转小写
void stringToLower(string& strTxt)
{
for (int i = 0; i < strTxt.length(); i++)
{
if (strTxt.at(i) >= 65 && strTxt.at(i) <= 90)
{
strTxt.at(i) += 0x20;
}
}
}
string StringConvert(const string &strSource, bool Islower)
{
int32_t i, len;
string str;
len = strSource.length();
if (Islower)
{
for (i = 0; i < len; i++)
{
if (strSource.at(i) == 0x20)
continue;
if (strSource.at(i) >= 0x41 && strSource.at(i) <= 0x5A) //A-Z
str += strSource.at(i) + 0x20;
else
str += strSource.at(i);
}
}
else
{
for (i = 0; i < len; i++)
{
if (strSource.at(i) == 0x20)
continue;
if (strSource.at(i) >= 0x61 && strSource.at(i) <= 0x7A) //a-z
str += strSource.at(i) - 0x20;
else
str += strSource.at(i);
}
}
return str;
}
//add hyb 修剪字符串
string TestStr(string& str)
{
int nPos = 0;
nPos = str.find_first_not_of(" ");
if (str.at(nPos) != string::npos)
{
str = str.substr(nPos, str.length() - nPos);
}
nPos = str.find_last_not_of("");
if (str.at(nPos) != string::npos)
{
str = str.substr(0, nPos + 1);
}
return str;
}
//实现String连接
string MyStrcat(string& str1, string& str2)
{
string st = { '\0' };
st = str1 + str2;
return st;
}
//实现String复制
string MyStrcpy(string& str1, string& str2)
{
string st = { '\0' };
str1 = str2;
return str1;
}
//实现String比较
int MyStrcmp(char* str1, char* str2)
{
int i = 0;
while ('\0' != *(str1 + i) && '\0' != *(str2 + i) && *(str1 + i) == *(str2 + i))
{
i++;
}
return *(str1 + i) - *(str2 + i);
}
int main(void)
{
string num1 = " hello WORLD";
string num2 = " HELLO world";
stringToHight(num1);
stringToLower(num2);
TestStr(num1);
TestStr(num2);
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
string str1 = "hello";
string str2 = "world";
string str3 = "one";
string str4 = "two";
char ch1[20] = {0};
char ch2[20] = { 0};
printf("输入第一个字符串: ");
fgets(ch1, 20, stdin); //回车也会读进去
printf("输入第二个字符串: ");
fgets(ch2, 20, stdin);
cout << "MyStrcat(str1, str2): " << MyStrcat(str1, str2) << endl;
cout << "MyStrcpy(str3, str4): " << MyStrcpy(str3, str4) << endl;
cout << "MyStrcmp(ch1,ch2): " << MyStrcmp(ch1, ch2) << endl;
}