As an assignment for school, I'm attempting to rewrite the string class. I've gotten a little bit of a block on inserting a string (array of characters) within another string (array of characters) at a specific point (index). I have created a class type of MyString and the content parameter is _string. Here's what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void MyString::Insert(constchar *CString, int index)
{
MyString temp; // define a temp area as a MyString object
temp._string = newchar[ strlen(_string) + strlen(CString) ];
for ( int i = 0; i < (index+1); i++ ) // copy the first part of the
temp._string[i] = _string[i]; // original string
for ( int j = 0; j < strlen(CString); j++) // copy the string to insert
temp._string[j+index] = CString[j]; // into the temp string
for ( int k = index+1; k < strlen(_string); k++) // resume copying the
temp._string[k+strlen(CString)] = _string[k]; // original string
strcpy(_string, temp._string); // copy the temp string as the original
delete [] temp._string; // release the memory for temp string
}
I appreciate any comments or suggestions.
Matt Myers
UAT Student