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 ;}
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:
-
char *p = line;This line initializes a pointerpto the start of the stringline. -
while(*p) p++;This loop moves the pointerpto the end of the string. -
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. -
p--;This line moves the pointerpback by one character, so it points to the last character of the string. -
while(*p == ' ' || *p == '\n' || *p == '\t') p--;This loop moves the pointerpback until it points to a character that is not a space, newline, or tab. -
p[1] = '\0';This line sets the character after the last non-whitespace character to the null character\0, effectively trimming the string. -
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.
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:
-
char *p = line;This line initializes a pointerpto the start of the stringline. -
while(*p) p++;This loop moves the pointerpto the end of the string. -
if(p == line) return 0;If the pointerpis still at the start of the string, this means the string is empty, so the function returns 0. -
p--;This line moves the pointerpone character back, to the last character of the string. -
while(*p == ' ' || *p == '\n' || *p == '\t') p--;This loop moves the pointerpback until it points to a character that is not a space, newline, or tab. -
p[1] = '\0';This line sets the character after the last non-whitespace character to the null character, effectively trimming the string. -
return p - line + 1;This line calculates and returns the length of the trimmed string.
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
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.