Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trim function halve the memory size to remove the whitespaces?

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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; 
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading