Question: Problem with C++ program. Visual Studio say when I try to debug Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I

Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work without showing debugging errors. And in other debugging tools such as code blocks it works fine but for this assignment I need to use visual studio. The program does not need to use any of C-library such as strlen. I need to make my own function to swap the arrays. Thanks.

This is the part of the code where I have trouble:

#include

using std::cout;

using std::cin;

using std::endl;

int myStrLen(const char*);

void myStrCpy(char*, const char*);

int myStrCmp(const char*, const char*);

void myStrSwap(char*, char*);

void myStrUpr(char*);

void myStrLwr(char*);

int main()

{

//STEP 3

char string3a[] = { "Those" };

char string3b[] = { "This" };

cout << "string3a = " << string3a << endl;

cout << "string3b = " << string3b << endl;

if (myStrCmp(string3a, string3b) == 0)

cout << "They are the same!" << endl << endl;

else

cout << "They are NOT the same!" << endl << endl;

//STEP 4

char string4a[] = { "Elsere" };

char string4b[] = { "Solohjj" };

cout << "string4a = " << string4a << endl;

cout << "string4b = " << string4b << endl;

myStrSwap(string4a, string4b);

cout << "string4a = " << string4a << endl;

cout << "string4b = " << string4b << endl << endl;

}

//STEP 3

int myStrCmp(const char* str1, const char* str2)

{

int i = 0; //initializes the integer array to 0 necessary for the loop to work

//loop to count ever character in the array and determine if they are not

//the same to return the value of '1'

for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++)

{

if (str1[i] != str2[i]) //if the characters from the arrays 'char string3a[]' and

{ //'char string3b[]' are not the same, it will return 1

return 1;

}

}

//loop to count every character in the array and determine if they have the same

//words and the same length before it encounters the null character

if (str1[i] == '\0' && str2[i] == '\0')

{

return 0; //if they have the same length and the same words it will return 0

} //if not, it will continue to return 1

else

{

return 1;

}

}

//STEP 4

void myStrSwap(char* str1, char* str2)

{

//loop to count every character in the arrays from main.

//the loop will swap every character and finishes until it reaches the null

//character in the arrays

int index = -1;

for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)

{

// if string 1 ends

if (str1[i] == '\0')

{

// find the position that string1 ends

if (index == -1)

index = i;

str1[i] = str2[i]; // copy the string2 data

// if the next position is end of string

if (str2[i + 1] == '\0')

{

// end both the string

str1[i + 1] = '\0';

str2[index] = '\0';

break; // break the loop

}

}

// if string 2 ends

else if (str2[i] == '\0')

{

// find the position that string1 ends

if (index == -1)

index = i;

// copy the string1 data

str2[i] = str1[i];

// if the next position is end of string

if (str1[i + 1] == '\0')

{

// end both the string

str2[i + 1] = '\0';

str1[index] = '\0';

break; // break the loop

}

}

else

{

char temp = str1[i];

str1[i] = str2[i];

str2[i] = temp;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!