Knowee
Questions
Features
Study Tools

The following function claims to "trim" the input string, of trailing whitespace (blank, newline, tab). Complete it.(Note: it claims to do the same job, as done by the trim() function in KnR,on page 65).The function, additionally, returns the length of the trimmed string.In every answer, ensure that you DO NOT use space.int trim(char *line) {    char *p = line;    while(*p)        Answer ;    Answer ;    if(p == Answer )        return Answer ;    while(*p == ' ' || *p == '\n' || *p == '\t')        Answer ;    p[1] = Answer ;    return p - Answer + Answer ;}

Question

The following function claims to "trim" the input string, of trailing whitespace (blank, newline, tab). Complete it.(Note: it claims to do the same job, as done by the trim() function in KnR,on page 65).The function, additionally, returns the length of the trimmed string.In every answer, ensure that you DO NOT use space.int trim(char *line) {    char *p = line;    while(*p)        Answer ;    Answer ;    if(p == Answer )        return Answer ;    while(*p == ' ' || *p == '\n' || *p == '\t')        Answer ;    p[1] = Answer ;    return p - Answer + Answer ;}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is the completed function:

int trim(char *line) {
    char *p = line;
    while(*p)
        p++;
    if(p == line)
        return 0;
    p--;
    while(*p == ' ' || *p == '\n' || *p == '\t')
        p--;
    p[1] = '\0';
    return p - line + 1;
}

Here is the step by step explanation:

  1. char *p = line; This line initializes a pointer p to the start of the string line.

  2. while(*p) p++; This loop moves the pointer p to the end of the string.

  3. if(p == line) return 0; This line checks if the string is empty (i.e., the start of the string is also the end). If it is, the function returns 0.

  4. p--; This line moves the pointer p back by one character, so it points to the last character of the string.

  5. while(*p == ' ' || *p == '\n' || *p == '\t') p--; This loop moves the pointer p back until it points to a character that is not a space, newline, or tab.

  6. p[1] = '\0'; This line sets the character after the last non-whitespace character to the null character \0, effectively trimming the string.

  7. return p - line + 1; This line calculates and returns the length of the trimmed string. It subtracts the address of the start of the string from the address of the last non-whitespace character and adds 1.

This problem has been solved

Solution 2

The function you're asking about is written in C language. Here's how you can complete it:

int trim(char *line) {
    char *p = line;
    while(*p)
        p++;
    if(p == line)
        return 0;
    p--;
    while(*p == ' ' || *p == '\n' || *p == '\t')
        p--;
    p[1] = '\0';
    return p - line + 1;
}

Here's a step-by-step explanation of the function:

  1. char *p = line; This line initializes a pointer p to the start of the string line.

  2. while(*p) p++; This loop moves the pointer p to the end of the string.

  3. if(p == line) return 0; If the pointer p is still at the start of the string, this means the string is empty, so the function returns 0.

  4. p--; This line moves the pointer p one character back, to the last character of the string.

  5. while(*p == ' ' || *p == '\n' || *p == '\t') p--; This loop moves the pointer p back until it points to a character that is not a space, newline, or tab.

  6. p[1] = '\0'; This line sets the character after the last non-whitespace character to the null character, effectively trimming the string.

  7. return p - line + 1; This line calculates and returns the length of the trimmed string.

This problem has been solved

Similar Questions

Which method can be used to remove any whitespace from both the beginning and the end of a string?

The following statements are TRUE about trim( ), EXCEPT:Removes whitespace from a stringPHP built-in functionRemove characters from both sides of a stringDeclared at the beginning of a file

40Which of the following methods can be used to remove whitespace from both ends of a string?Review Laterreplace()slice()split()trim()

The following function is an imitation of the atof() function given in KnR  in section 4.2Complete it. Do not use space in any answer.double atof(char *str) {    double whole = 0.0, power = Answer ;    int sign;    while(Answer && (*str == ' ' || *str == '\t'))        str++;    sign = (*str == '-' ? -1 : Answer );    (*str == '-' || *str == '+') ? Answer : NULL;        while(*str && (*str >= Answer && *str <= Answer ))        whole = whole * Answer + (Answer - Answer );    *str == Answer ? str++: NULL;    while(*str && (*str >= Answer && *str <= Answer )) {        whole = Answer * 10.0 + (Answer - '0');        power *= 10.0;    }       return sign * whole / power;}

Scanner nextLine() _____________  leading spaces and tabs in the returned string.Group of answer choicesskipsincludes

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.