Skip to content

Commit 6cb67cb

Browse files
committed
[Chap12][String] redesign string
1 parent 3d21d37 commit 6cb67cb

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

12_chap_mem/string/.string.cpp.swp

12 KB
Binary file not shown.

12_chap_mem/string/.test.cpp.swp

12 KB
Binary file not shown.

12_chap_mem/string/string.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "string2.h"
2+
#include <iostream>
3+
#include <cstring>
4+
5+
using namespace std;
6+
7+
String :: String ()
8+
{
9+
cout << "default constructors!" << endl;
10+
}
11+
String :: String (const string s1)
12+
{
13+
if (s1.length() > 0)
14+
{
15+
strcpy(raw,s1.c_str(),s1.length());
16+
}
17+
}
18+
19+
String String::operator= (const String s1) const
20+
{
21+
if (strlen(s1.raw) > 0)
22+
{
23+
strcpy(raw,s1.raw,strlen(s1.raw));
24+
}
25+
26+
return *this;
27+
}
28+
29+
String String::operator+ (const String s1) const
30+
{
31+
String temp;
32+
33+
if (strlen(s1.raw) > 0)
34+
{
35+
strcpy(temp.raw,s1.raw,strlen(s1.raw));
36+
}
37+
strcpy(temp.raw + strlen(s1.raw),raw,strlen(raw));
38+
39+
return temp;
40+
}
41+
42+
void String::stringup()
43+
{
44+
char temp;
45+
46+
if (strlen(raw) > 0)
47+
{
48+
for (int = 0 ; i < strlen(raw); i++)
49+
{
50+
if (raw[i] >= 'a' && raw[i] <= 'z')
51+
{
52+
raw[i] = raw[i] + '0'; //to high characters
53+
}
54+
}
55+
}
56+
}
57+
58+
void String::stringlow ()
59+
{
60+
61+
if (strlen(raw) > 0)
62+
{
63+
for (int = 0 ; i < strlen(raw); i++)
64+
{
65+
if (raw[i] >= 'A' && raw[i] <= 'Z')
66+
{
67+
raw[i] = raw[i] - '0'; //to low characters
68+
}
69+
}
70+
}
71+
}
72+
73+
int String :: has (const char* c) const
74+
{
75+
int cout = 0;
76+
77+
if (c==NULL)
78+
{
79+
cout << "wrong format of c \n";
80+
return 0;
81+
} else
82+
{
83+
for (int i = 0; i<strlen(raw),i++)
84+
{
85+
if (raw[i] == c)
86+
{
87+
count++;
88+
}
89+
}
90+
}
91+
92+
return count;
93+
}

12_chap_mem/string/string2.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
class String
3+
{
4+
private:
5+
enum {MAX=128;};
6+
char raw[MAX];
7+
public:
8+
String ();
9+
String (const string s1);
10+
String operator=(const String s1) const;
11+
String operator+(const String s1) const;
12+
void stringlow();
13+
void stringup();
14+
int has (const char* c) const;
15+
};

12_chap_mem/string/test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
#include "string2.h"
5+
6+
int main ()
7+
{
8+
String s1 ("and I am a C++ student. ");
9+
String s2 = "Please enter your name :";
10+
String s3;
11+
String ans;
12+
bool success = false;
13+
14+
cout << s2;
15+
cin >> s3;
16+
s2 = "My name is " + s3;
17+
cout << s2 << ".\n";
18+
s2 = s2 + s1;
19+
s2.stringup();
20+
cout << "The string \n" << s2 << "\n contains " << s2.has('A')
21+
<< "'A' characters in it. \n";
22+
s1 = "red" ;
23+
String rgb[3] = { String(s1) ,String ("green"),String("blue")};
24+
cout << "Enter the name of a primary color for mixing light: ";
25+
while (cin >> ans)
26+
{
27+
ans.stringlow();
28+
for (int i=0; i< 3; i++)
29+
{
30+
if (ans == rgb[i])
31+
{
32+
cout << "That's right !\n";
33+
success = true;
34+
break;
35+
}
36+
}
37+
if (success)
38+
break;
39+
else
40+
cout << "Try again! \n";
41+
42+
}
43+
44+
cout << "Bye\n";
45+
return 0;
46+
}

0 commit comments

Comments
 (0)