Home > Back-end >  trim function halve the memory size to remove the whitespaces?
trim function halve the memory size to remove the whitespaces?

Time:11-28

I have to create a function (trim function) who can perform this task: taking a null terminated string and if at the 0-th position of the string there's a whitespace, remove that whitespace. Same thing if the whitespace is at the end of the string (before the zero terminator). Therefore, basically the function ignores the whitespaces in the middle of the string.

Here is what I tried to do so far, (1) I passed " a b " string to trim function. (2) (null pointer check). (3) I took the length of the string by using strlen function.

(4) (this is the delicate part, because debugging line-by-line I found a strange error inside the for loop).

the error is this: when debugger runs the first line of the for loop, it goes inside the loop as expected; okay, that's fine, but when the debugger runs the if check, it should be true (because at the beginning of the string there's a whitespace) and the function is supposed to go in the if body, in the first if statement, and reallocate the memory. But that's not true, because realloc is never executed. Why?

(the function must return the pointer to the reallocated memory).

another error is that "s" isn't initialized but I used it anyway, and that's not true because I initialized s with " a b " string.

char* trim(const char* s) {    
    if (s == NULL) {  
        return NULL; 
    } 
    size_t length = strlen(s);
    for (unsigned int i = 0; s[i] != '\0'; i  ) { 
            

        if (s[i] == " ") {
            realloc(s, length - sizeof(char));       
        } 
    }
    return s; 

}
int main(void) {

    
    trim(" a b ");

    return 0; 
}

CodePudding user response:

In the if statement you are trying to compare the object of the type char s[i] with the string literal " " that is implicit;ly converted to a pointer to its first element.

   if (s[i] == " ") {

It is evident that such a comparison evaluates to logical false.

You need to use the integer character constant ' ' instead of the string literal.

   if (s[i] == ' ') {

Also this memory reallocation

realloc(s, length - sizeof(char));       

does not make a sense because at least the address of the reallocated memory is stored nowhere.

You shall not touch the source string s. You need to allocate dynamically a new character array and copy there the trimmed source string.

The function can be defined for example the following way as it is shown in the demonstration program below.

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

char * trim( const char *s ) 
{
    const char *first = s;
    
    while ( isblank( ( unsigned char )*first ) )   first;
    
    const char *last = first   strlen( first );
    
    while ( last != first && isblank( ( unsigned char )last[-1] ) ) --last;

    size_t n = last - first;
    
    char * result = malloc( n   1 );
    
    if ( result != NULL )
    {
        memcpy( result, first, n );
        result[n] = '\0';
    }
    
    return result;
}

int main(void) 
{
    const char *s = " a b ";
    
    printf( "\"%s\"\n", s );
    
    char *p = trim( s );
    
    printf( "\"%s\"\n", p );

    free( p );
    
    return 0;
}

The program output is

" a b "
"a b"

Within the function instead of this while loop

while ( isblank( ( unsigned char )*first ) )   first;

you can use the function strspn as

first  = strspn( first, " \t" );
  • Related