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

2. Create a function named “trim” that will trim all blanks IN FRONT and BEHIND

ID: 3634338 • Letter: 2

Question

2. Create a function named “trim” that will trim all blanks IN FRONT and BEHIND a string. This function that will accept one parameter (string original) and will RETURN the new string that is smaller in size and trimmed.

Ex.

char original[] = “ Mr. Bahndi needs to get a life? ”;
char answer[] = trim(original);
printf(“%s ”, answer);

// answer will equal “Mr. Bahndi needs to get a life?”

char[] trim(char x[40])
{





return x;
}

ALSO WE HAVEN'T GONE OVER FLAGS IN CLASS. IS THERE ANOTHER WAY OF GOING ABOUT THIS? IF NOT, WHATEVER WORKS IS FINE

Explanation / Answer

Dear,

#include <stdio.h>
#include <string.h>

char* trim(char x[40]);

int main()
{
char original[] = " Mr. Bahndi needs to get a life? ";
char *answer = trim(original);
printf("%s ", answer);
return 0;
}

char* trim(char x[40])
{
    char *str = x;
    int j;
    int len = strlen(x);

    // this loop removes leading spaces
    while(1)
    {
        if(x[0] == ' ')
        {
            for(j = 0; j < len-1; j++)
                x[j] = x[j+1];
           
            x[j] = '';
            len--;
        }   
        else
            break;
    }

    //this loop removes trailing spaces
    len = strlen(x);
    while(true)
    {
        if(x[len-1] == ' ')
        {
            x[len-1] = '';
            len--;
        }
        else
            break;
    }

    //returns shorted length string
    return str;
}

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