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
|
#include <iostream>
using namespace std;
class String
{
private:
char* str;
public:
String(char* s = NULL);
~String();
void set(char *s);
String& insert(int index, const char*other, String* y);
};
String::String(char *s)
{
if (s)
{
int l = strlen(s) + 1;
str = new char[l];
strcpy_s(str, 100, s);
}
else
str = NULL;
}
String :: ~String()
{
if (str)
{
delete[] str;
str = NULL;
}
}
void String::set(char*s)
{
if (s)
{
int l = strlen(s) + 1;
str = new char[l];
strcpy_s(str, 100, s);
}
}
String& String::insert(int index, const char* other, String* y)
{
int n = strlen(str) + strlen(other) + 1;
char* tmp = new char[n];//New dynamic space allocation.
strcpy_s(tmp, 100, other);//Copy the string are sent to help.
strcpy_s(tmp + index, 100, str);/*Copying string ptr end String helped without overwriting.*/
strcpy_s(tmp + strlen(tmp), 100, other + index);/*Copying the end of the word string sent without overwriting data.*/
*y = tmp;
delete[]tmp;
return *y;
}
int main()
{
String a, b;
char * t = new char[1000];
char * r = new char[1000];
cin >> t;
cin >> r;
a.set(t);
b.set(r);
return 0;
}
|