Remove all whitespaces from String in C Language

Program-Remove-all-whitespaces-from-String-in-C-Language

Remove All Whitespaces from String in C Program Description:

Write a Program to Remove All Whitespaces from String in C programming language. The program should accept a string from the user and string may contain leading, Trailing, and even the whitespace between the words. The program should remove all whitespace(unnecessary whitespaces) from the string and return the resultant string.

Example Input and Output:

Input:

Enter a string :           Making       Mistakes      means    you're    learning   fast!

Output:

In the above example, The input string has leading, and trailing whitespaces and even there are a few extra whitespaces in between the string. The program should remove all whitespaces and return the resultant string. ( The resultant string will contain one whitespace character between the characters)

Program Prerequisites:

We are going to use the C-String, C-Arrays, and Functions in C in the following program. It is good idea to know the basics of these concepts to understand the program better.

Remove All Whitespaces from String in C Program Explanation:

As we have specified earlier, The input string might contain Leading whitespaces, Trailing whitespace, and the whitespaces in between the words. So we need to handle all the above cases and remove all whitespaces.

We have already looked at the programs to

We are going to utilize the above three programs to remove all whitespace characters from a string/sentence.

In the following program, We have defined three user-defined functions. They are trimLeadingWSpaces() function, trimTrailingWSpaces() function, and removeWSpacesBWWords() function. Let’s look at the details of each function.

The trimLeadingWSpaces() function:

  • Function Prototype: void trimLeadingWSpaces(char * str)
  • This function is used to remove the Leading whitespaces from the given string( str).
  • To Remove the Leading whitespace from the string, Shift the valid characters(non-whitespace characters) to the start(Zeroth Index) of the string. Please go through the Remove Leading Whitespaces Program for a detailed explanation.

The trimTrailingWSpaces() function:

  • Function Prototype: void trimTrailingWSpaces(char * str)
  • This function will remove all trailing whitespaces from the given string str.
  • To Remove the Trailing Whitespaces, We will search for the first non-whitespace character in a string in a backward direction, and after we discovered the last non-whitespace character. Then we will update it with the terminating NULL character. Again, Go through the Remove Trailing Whitespaces Program for a detailed step-by-step explanation.

The removeWSpacesBWWords() function:

  • Function Prototype: void removeWSpacesBWWords(char *inputStr, char * outputStr)
  • This function takes two formal arguments i.e inputStr and outputStr. The removeWSpacesBWWords() function removes all unnecessary whitespaces between the words and stores the resultant string in the outputStr string. Please go through the Remove extra whitespaces in between words in a String program for more details.
  • We can also return a string from this function instead of getting the outputStr as argument.

In the main() function, Create two strings called inputStr and outputStr. Take the user input and update the inputStr. Then Call the above three functions one by one to remove all whitespaces from the string.

The first two functions, The trimLeadingWSpaces() and trimTrailingWSpaces() functions changes the inputStr. As we are passing the string by address or reference all the changes made in the functions are visible in the main() function.

Finally, display the resultant string on the console.

Program to Remove All Whitespaces from String in C:

Here is the program to Remove All whitespaces from a String in C Programming Language.

Program Description:

Let’s Compile and Run the Program.

Compile the Program.

Run the Program.

Test Case 1: When the input string has Leading Whitespaces

As we can see from the above output, The program removed the reading whitespaces from the input string.

Test Case 2: String with Trailing Whitespaces

In the above example, The input string has trailing whitespaces and the program removed the trailing whitespaces and provided the string without extra spaces.

Test Case 3: String with Whitespaces between words

As we can see from the above output, The input string Learn      Programming         Online has a few whitespace characters between the words and the program removed all extra whitespaces between the strings and displayed the output string on the console.

Test 4: When the input string contains Leading Whitespaces, Trailing Whitespaces, and whitespaces between the words.

Remove-all-whitespaces-from-String-in-C-Program-Output

The program removed all whitespaces from a string and returned the resultant string.

Related String Programs:

Venkatesh

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 All Whitespaces in a String or Sentence […]

  2. […] C Program to Remove All Whitespaces in a String or Sentence […]

Leave a Reply