Program to Remove Trailing Whitespace in String in C Language

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

Program Description:

Write a Program to Remove Trailing Whitespace in C programming language. The program will accept a string from the user and removes or trims all the trailing white spaces from the input string.

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

Excepted Input and Output:

Input:

Enter a string : Programming is Fun

Output:

Before removing Trailing Whitespaces:Programming is Fun (Actual string)

After removing Trailing Whitespaces:Programming is Fun(Resultant string)

In the above example, The input string has trailing spaces, The program should remove the trailing spaces and return the valid NULL-terminated string.

Program Prerequisites:

It is recommended to have knowledge of the C Strings, Arrays, and Functions. Please go through the following article to learn more about them.

Remove Trailing Whitespace in a String in C Program Explanation:

Let’s look at the step-by-step instructions of the program.

  1. Create an integer constant named SIZEwith the value 100. This is the max size of the input string. ( Please 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 the 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 check boundaries.
  4. Create a variable called endand initialize it with the last character in the string i.e strlen(inputStr) - 1. This variable is used to hold the index of the last non-whitespace character in the inputStr
  5. Create a While Loop and Iterate over the input string. Start the loop from the endindex (the last character in the string) and check if the character at the endindex is a white space. We use isspace() function from the ctype.h header file.
    • If the character at the index endis a white space, Then decrement the endby 1 – i.e end--;
    • Repeat this step till we find a non-whitespace character in the string.
  6. Once Step 5 is completed, The endindex will be pointing to the last non-whitespace character in the string. So this will be the actual end of the string.
  7. To Remove the Trailing whitespace from the string, Simply, Add the Terminating NULL Character ( '\0') at the end+1 index. – i.e inputStr[end+1] = '\0';
  8. Print the Resultant string on Console.

Program to Remove Trailing Whitespace in String in C:

Here is the program to Remove the Trailing white spaces from a string in C programming language.

Program Output:

Compile and Run the Program.

remove-trailing-spaces-program-output

Let’s try one more example with Tabs:

As we can see from the above output, The program is properly removing the trailing white spaces from the input string. Note that, We intentionally added the (Actual String) at the end of the string to represent the spaces.

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

In this method, We are going to create a user-defined function to remove the trailing white spaces in the input string.

Here is the program to remove the Trailing whitespace using a function named trimTrailingWSpaces. The trimTrailingWSpaces() function takes a string as the input and removes the all trailing or ending white spaces.

We have called the trimTrailingWSpaces(inputStr) function with the inputStr from the main() function to remove the trailing whitespace from the input string ( inputStr).

The Prototype of the trimTrailingWSpaces() function is char * trimTrailingWSpaces(char * str). This function takes the input string as the formal argument and removes the trailing spaces and returns the resultant string back to the caller.

Program Output:

Let’s compile and Run the Program and observe the output.

trim-trailing-spaces-using-functions-in-c

The function is properly removing the trailing white spaces.

Related Articles:

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

3 Responses

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

  2. […] String with trailing spaces […]

  3. […] C Program to Remove Trailing Whitespaces in a String […]

Leave a Reply