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. Thanks

This is part of the code where I have problems

#include using std::cout; using std::cin; using std::endl;

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

int main()

{

char string3a[] = { "Those" };

char string3b[] = { "That" };

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;

char string4a[] = { "Elseredsas" };

char string4b[] = { "Solohjlkllhh" };

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

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

myStrSwap(string4a, string4b);

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

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

}

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

{

int i = 0;

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

{

if (str1[i] != str2[i])

{

return 1;

}

}

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

{

return 0;

}

else

{

return 1;

}

}

void myStrSwap(char* str1, char* str2)

{

char index = -1;

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

{

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

{

if (index == -1)

index = i;

str1[i] = str2[i];

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

{

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

str2[index] = '\0';

break;

}

}

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

{

if (index == -1)

index = i;

str2[i] = str1[i];

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

{

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

str1[index] = '\0';

break; // break the loop

}

}

else

{

char temp = str1[i];

str1[i] = str2[i];

str2[i] = temp;

}

}

}

}

Thanks

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!