C program to trim leading white spaces from a string

In this blog post, you will learn how to write a C program to trim leading white spaces from a string. How to remove both leading white space characters in a string using loop in C programming. Also, logic to delete all leading white space characters from a given string in C.

I will take the help of the isspace function to find the whitespace character within the given input string. So let’s see the C program to trim leading white spaces from a string.

Examples:

Input String with leading whitespace:> "     aticle world .com"


Output String:> "aticle world .com"

 

 

C program to trim leading and trailing white spaces from a string:

Let’s some ways to trim leading whitespace characters from the given input string. You can use it according to your use.

If you can modify the input string:

The below function returns a pointer to a substring of the original string. Also If the given string was allocated dynamically, the programmer should use the original pointer to deallocate the allocated memory. They must not be used the return pointer for deallocating the memory.

Method 1:

Algorithm:

1. Traverse the given string character by character and passed it into the isspace function.

2. Increment the pointer variable whenever encounter the whitespace letter.

3. Break the loop when encountered the null character (Limitation there must not be another null character in the string except the terminating null character) and any non-whitespace character.

4. Return the updated pointer from the function. It now removed all leading whitespace characters.

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

char *trimLeadingWhiteChar(char *str)
{
    if ((str != NULL) && (*str != '\0'))
    {
        // Trim leading space
        while((*str != '\0') && isspace((unsigned char)*str))
        {
            ++str;
        }

    }
    return str;
}


int main()
{
    char str[] = "     aticle world .com";

    printf("\nString before trimming Leading white space: \n'%s'\n\n", str);

    char *p = trimLeadingWhiteChar(str);

    printf("\n\nString after trimming Leading white spaces: \n'%s'\n\n", p);

    return 0;
}

Output:

trim leading whitespace char in c

 

 

Method 2:

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

void trimLeadingWhiteChar(char* str)
{
    if ((str != NULL) && (*str != '\0'))
    {
        // First remove leading spaces
        const char* firstNonSpace = str;
        while(*firstNonSpace != '\0' && isspace((unsigned char)*firstNonSpace))
        {
            ++firstNonSpace;
        }
        unsigned int len = strlen(firstNonSpace)+1;
        memmove(str, firstNonSpace, len);
    }
}


int main()
{
    char str[] = "     aticle world .com";

    printf("\nString before trimming leading white space: \n'%s'\n\n", str);

    trimLeadingWhiteChar(str);

    printf("\n\nString after trimming leading white spaces: \n'%s'\n\n", str);

    return 0;
}

Output:

trim leading whitespace char in c

 

 

If you can not modify the input string:

This method is useful when you don’t want to modify the input string. In this method, we store the trimmed input string into the given output buffer, which must be large enough to store the result.

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


unsigned int trimLeadingWhiteChar(char *outputBuffer, unsigned int givenStringLen, const char *str)
{
    unsigned int  outputBufferSize = 0;
    if((str != NULL)
            && (givenStringLen > 0))
    {
        // Trim leading space
        while(*str != '\0' && isspace((unsigned char)*str))
        {
            ++str;
        }

        if(*str == 0)  // All spaces?
        {
            *outputBuffer = 0;
            outputBufferSize = 1;
        }
        else
        {
            outputBufferSize = strlen(str);

            // Copy trimmed string and add null terminator
            memcpy(outputBuffer, str, outputBufferSize);

            //Assign null character
            outputBuffer[outputBufferSize] = 0;
        }
    }

    return outputBufferSize;
}


int main()
{
    char str[] = "     aticle world .com";

    const unsigned int gievenStringSize = sizeof(str);
    char outputBuffer[gievenStringSize];

    printf("\nString before trimming leading white char: \n'%s'\n\n", str);

    printf("\n\nString len before trimming leading white char: \n%d\n\n", gievenStringSize);

    unsigned int lenSubString = trimLeadingWhiteChar(outputBuffer,gievenStringSize,str);

    printf("\n\nString after trimming leading white char: \n'%s'\n\n", outputBuffer);
    printf("\n\nString len after trimming leading white char: \n%d\n\n", lenSubString);

    return 0;
}

Output

C program to trim leading white spaces from a string

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *