Question: My question is in the code below, when I have the strcat(phrase, '0'), it breaks my code and gives me the error of SIGSEGV -
My question is in the code below, when I have the strcat(phrase, '\0'), it breaks my code and gives me the error of SIGSEGV - Segmentation violation signal. I don't know how to fix it and every time I try to fix it, it breaks the rest of my FRONT code, which works. If anyone can see why my code is breaking at that point, that would be much appreciated.
C++ Code (Bold is breaking):
//Write the ctrim function here char ctrim(char phrase[100], TRIMMETHOD trim) { int SIZE = (int)strlen(phrase); int i = 0; char *ptr = nullptr; ptr = phrase;
if (trim == TRIMMETHOD::BACK || trim == TRIMMETHOD::BOTH) { for (i = 0; i < SIZE; i++) { if (!isspace(phrase[i])) { strcpy(ptr, &phrase[i]); } } strcat(phrase, '\0'); }
if (trim == TRIMMETHOD::FRONT || trim == TRIMMETHOD::BOTH) { int i = 0; while (isspace(*ptr) && i < SIZE) { strcpy(ptr, &phrase[i]); i++; } phrase = ptr;
} return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
