Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a strtrim function using the provided interface that trims the whitespace

ID: 3875535 • Letter: W

Question

Write a strtrim function using the provided interface that trims the whitespace off the front and back of any null terminated cstring. Must work on all strings including but not limited to nullptr, empty strings, and lank strings. The function must work with heap allocated strings. The return must be identical to the input parameter. Be sure your function passes the provided test cases Your strtrim function may only use methods found in the following headers: #include #include You may use the cstring header, but only if you pay very close attention. nullptr, "Hello, world!", "whitespace", "whitespace right "whitespace left" " tab ", " r crlf ", " The quick brownInltvrlffox jumps over the lazy dog.  ", "Here comes the rain again", "Falling on my head like a memory", "Falling on my head like a new emotion", "I want to walk in the open wind", "I want to talk like lovers do" "I want to dive into your ocean", "Is it raining with you" 1;

Explanation / Answer

/**********C Program for triming the whitespaces************/

#include "stdafx.h"

#include <stdlib.h>

#define MAX_SIZE 100

void trimTrailing(char * str);

void trimLeading(char *str);

void removeBlanks(char * str);

int main()

{

char str[MAX_SIZE];

printf("Enter any string: ");

gets(str);

printf(" String before trimming : '%s'", str);

trimLeading(str);

trimTrailing(str);

removeBlanks(str);

  

printf(" String after trimming: '%s'", str);

return 0;

}

//Function run a loop till the last whitespace and shift characters to eliminate the space.

void trimLeading(char * str)

{

int index = 0;

while(str[index] == ' ' || str[index] == ' ' || str[index] == ' ')

{

index++;

}

if(index != 0)

{

int i = 0;

while(str[i + index] != '')

{

str[i] = str[i + index];

i++;

}

str[i] = '';

}

}

//Function run a loop if character is non-whitespace last non-whitespace index is found.Add null at index+1(Null terminated string and remove all Trailing White spaces)

void trimTrailing(char * str)

{

int index = -1;

int i = 0;

while(str[i] != '')

{

if(str[i] != ' ' && str[i] != ' ' && str[i] != ' ')

{

index= i;

}

i++;

}

str[index + 1] = '';

}

//Function finds multiple whitespaces and replace it with single whitespace

void removeBlanks(char * str)

{

int i = 0;

int j = 0;

while(str[i] != '')

{

if(str[i] == ' ')   

{

str[j] = ' ';

j++;

while(str[i] == ' ')

i++;

}

str[j] = str[i];

i++;

j++;

}

str[j] = '';

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote