-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_4_string_without_null_terminator.cpp
99 lines (79 loc) · 2.04 KB
/
4_4_string_without_null_terminator.cpp
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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
typedef char * arrayString;
void output(arrayString str);
void append(arrayString &str, char ch);
void concatenate(arrayString &str1, arrayString str2);
char characterAt(arrayString str, int position);
int main()
{
arrayString str = new char[5];
str[0] = 4; str[1] = 'T'; str[2] = 'e'; str[3] = 's'; str[4] = 't';
cout << "The string: ";
output(str);
cout << "\nCharacter at position " << 1 << " is " << characterAt(str, 1) << endl;
cout << "\nBefore append: ";
output(str);
append(str, '!');
cout << "\nAfter append: ";
output(str);
arrayString str1 = new char[5];
str1[0] = 4; str1[1] = 'T'; str1[2] = 'e'; str1[3] = 's'; str1[4] = 't';
arrayString strToAdd = new char[4];
strToAdd[0] = 3; strToAdd[1] = 'b'; strToAdd[2] = 'e'; strToAdd[3] = 'd';
cout << "\nBefore concatenate: ";
output(str1);
cout << "\nString to add: ";
output(strToAdd);
concatenate(str1, strToAdd);
cout << "\nAfter concatenate: ";
output(str1);
cout << "\n";
cout << (void*) str1 << " " << (void*)strToAdd << endl;
delete[] str;
delete[] str1;
delete[] strToAdd;
cin.get();
return 0;
}
void output(arrayString str)
{
for (int i = 1; i <= str[0]; i++)
{
cout << str[i];
}
}
char characterAt(arrayString str, int position)
{
return str[position];
}
void append(arrayString &str, char ch)
{
arrayString newStr = new char[str[0] + 2];
for (int i = 1; i <= str[0]; i++)
{
newStr[i] = str[i];
}
newStr[str[0]+1] = ch;
newStr[0] = str[0] + 1;
delete[] str;
str = newStr;
}
void concatenate(arrayString &str1, arrayString str2)
{
int newLen = str1[0] + str2[0];
arrayString newStr = new char[newLen+1];
for (int i = 1; i <= str1[0]; i++)
{
newStr[i] = str1[i];
}
for (int i = str1[0]+1; i <= newLen; i++)
{
newStr[i] = str2[i - str1[0]];
}
newStr[0] = newLen;
delete[] str1;
str1 = newStr;
}