Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

So, I know I posted a similar question earlier, but I was wondering how you can trim a cstring from both ends. I have my

So, I know I posted a similar question earlier, but I was wondering how you can trim a cstring from both ends. I have my code working from the front and back but putting it together is not working. The code I am working with is below, please tell me what part I am missing and what I need to do in order to understand what I am doing wrong:

#include "strFunctions.h"

char ctrim(char phrase[100] ,TRIMMETHOD trim);

void strim (char phrase[100] ,TRIMMETHOD trim);

enum TRIMMETHOD{ BOTH, FRONT, BACK};

char ctrim(char phrase[100], TRIMMETHOD trim) { int SIZE = (int)strlen(phrase); int i = 0; char *ptr = nullptr; ptr = phrase;

if (trim == TRIMMETHOD::FRONT) { while (isspace(*ptr) && i < SIZE) { strcpy(ptr, &phrase[i]); i++; } phrase = ptr; } if (trim == TRIMMETHOD::BACK) { for (i = (SIZE - 1); (isspace(phrase[i])); i--); { phrase[i + 1] = '\0'; } } if (trim == TRIMMETHOD::BOTH) { for (i = (SIZE - 1); (isspace(phrase[i])); i--); { phrase[i + 1] = '\0'; } while (isspace(*ptr) && i < SIZE) { //strcpy(ptr, &phrase[i]); i++; } phrase = ptr;

} return 0; }

void strim (char phrase[100] ,TRIMMETHOD trim)

{

}

If needed to debug, I provided one of the test cases as well:

TEST_CASE("ctrim both - Removing 10 spaces from both sides") { char original[100] = " Hi "; char answer[100] = "Hi"; ctrim(original, BOTH); REQUIRE(strcmp(answer, original) == 0);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started