Program to Remove Leading Whitespace in String in C Language

Spread the love
Program-to-Remove-Leading-Whitespace-in-String-in-C-Language

Program Description:

Write a Program to Remove Leading Whitespace in C programming language. The program should accept a string from the user and Trim the Leading whitespace or Remove the Leading whitespace from the input string. Let’s look at the example input and output of the program.

📢 This Program is part of the C String Practice Programs series

Example Input and Output:

Input:

Enter a string :                                   Lot of Leading spaces

Output:

String after removing Leading Whitespaces:Lot of Leading spaces

The input string contains a few whitespace characters at the beginning, but the program removed all leading whitespaces, So the output string contains no leading white spaces.

Prerequisites:

We are going to use the C-Strings and Functions, So please go through the following articles to learn more about these topics.

Let’s look at the step-by-step explanation of the program to trim leading white spaces in C.

Remove Leading Whitespaces in String in C Program Explanation:

  1. Create an integer constant named SIZE with the value 100. This is the max size of the character array. ( Change this value if you want to use large strings)
  2. Create a character array i.e string to store the user input. Let’s say we created a inputStr string with size of SIZE elements.
  3. Take the input string( inputStr) from the user and Read it using the gets function. We can also use other functions like scanf, fgets, etc. Also, note that the gets function doesn’t do the boundary checks.
  4. Create variable called start and initialize it with Zero(0). This variable holds the index of the first non-whitespace character in the inputStr
  5. Create a While Loop and Iterate over the string from the start and check if the character at the start index is a white space. We use isspace() function from the ctype.h header file.
    • If the character at the index start is white space, Then increment the start by 1 – start++
    • repeat this step till we find the first non-whitespace character in the string.
  6. Once Step 5 is completed, The start index will be pointing to the first non-whitespace character in the string.
  7. To Remove the Leading whitespace from the string, Shift the valid characters(non-whitespace characters) to the start(Zeroth Index) of the string.
  8. Create a For loop and Shift the character’s(non-whitespace) start of the string. – for(i = 0; inputStr[i]; i++)
    • At each iteration, Replace the element at the index i with the character at the index i+start – i.e inputStr[i] = inputStr[i+start];
  9. Finally, Add the terminating NULL Character ( '\0') to the inputStrinputStr[i+1] = '\0';
  10. Print the Resultant string on Console.

Program to Remove Leading Whitespace in C Language:

Here is the Program to Remove Leading Whitespace in C using Iterative method.

Program Output:

Compile and Run the program using your favorite IDE. We are using the GCC compiler in this example.

Remove-Leading-Whitespace-in-C-Language-Program-output

Let’s try with the Tabs

As we can see from the above user input strings and Output strings, The program is able to remove the leading whitespaces successfully.

Remove Leading Whitespace in String in C using a user-defined function:

We used only a single function to do all the tasks in the above program. Let’s divide the above program and create a function to remove the leading whitespaces in the given string.

The function should accept a string from the caller and remove the leading whitespaces and return the resultant string back to the caller.

Here is the modified version of the above program, Where we created a user-defined function named trimLeadingWSpaces to trim or Remove the leading whitespace in the input string.

We have defined the trimLeadingWSpaces() function in the above program.

  • Prototype of the Function : char * trimLeadingWSpaces(char * str)
  • This function takes a Formal Argument i.e the input string (str) and removes all leading characters from the str and returns the resultant string str back to the caller.

In the main() function, we called the trimLeadingWSpaces(inputStr) with the inputStr as the actual argument.

Program Output:

let’s compile and Run the program.

Remove-Leading-Whitespace-using-function-in-c

The program is providing the desired results.

Related String practice programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Remove Leading Whitespaces in a String […]

  2. […] C Program to Remove Leading Whitespaces in a String […]

Leave a Reply