Program to Remove extra spaces between the words in C

Program-to-Remove-extra-spaces-between-the-words-in-C

Program Description:

Write a Program to Remove extra spaces between the words in a String using C Programming language. The program should accept a string or sentence from the user, The input string may contain extra whitespace (spaces, tabs, etc) in between the words. The program should remove all extra whitespace between the words and return the resultant sentence. The resultant sentence will have only one space between the words.

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

Here is the program expected input and output

Example Input and Output:

Input:

Enter a string : Learn Programming Online

Output:

Before Removing Whitespaces:Learn Programming Online(Actual string)

After Removing Whitespaces:Learn Programming Online(Resultant string)

If you observe the resultant string only has one space between the words and all extra spaces are removed.

Program Prerequisites:

It is recommended to have knowledge of the string, arrays, and functions in C. Please go through the following articles to learn about these concepts.

Let’s look at the step-by-step explanation of the program to remove extra spaces between the words in a sentence.

Remove extra spaces between the words in a String Program Explanation:

  1. Create a Macro named SIZE to store the maximum number of characters in the input string. We defined the Max size as 100. Please change this value to suit your requirements.
  2. Declare an input string with the SIZE number of characters. Let’s say this string as inputStr. Also, create a Resultant string named resultStr with SIZE characters.
  3. Prompt the user to provide the input string and read it using the gets function and update the inputStr string with the given string.
  4. To Remove Extra spaces between the words in a String, We will use two indices called i and j. The i index will be used to traverse the input string( inputStr) and The j index is used to update the Resultant string( resultStr).
  5. Traverse the input string and Skip the character if two consecutive characters contain a whitespace character, Otherwise, update the current character in the resultStr string.
  6. Start a For Loop to traverse the string. Start with i = 0, j = 0 and continue till we reach the terminating NULL character inputStr[i] != '\0'
    • At each iteration check if two consecutive characters are whitespaces using the isspace() function from the ctype.h library. We can also manually compare the characters.
      • If the above condition is true, The skip the character and continue to the next iteration
    • If the above condition is false, Then update the resultStr[j] with inputStr[i]. Increment the both i and j indices. i.e resultStr[j++] = inputStr[i];
  7. Finally, Add the Terminating NULL character to the resultant string at the index j. i.e resultStr[j] = '\0';
  8. Print the results on the console.

Program to Remove extra spaces between the words in C:

Here is the program to Remove Extra spaces between the words in a given string in the C programming Language.

As we are using the isspace() function, We need to include the ctype.h header file.

Program Output:

Compile and Run the Program using your favorite compiler

remove-extra-spaces-between-words-program-output

Let’s try a few more examples.

As we can see from the above outputs, The input string has white spaces in between the words and the program removed the spaces and returned the string without any extra spaces.

For example, When we provided the string C          Programming              Online as the input string, Program removed the extra spaces and returned the C Programming Online as the resultant string.

Remove extra spaces between the words in String using User defined function:

Let’s move the logic to remove extra spaces into a dedicated function So that we can reuse the code. In this program, We will create a function named removeWSpacesBWWords to remove the whitespace characters between the words.

Here is the prototype of the removeWSpacesBWWords() function.

void removeWSpacesBWWords(char *inputStr, char * outputStr)

The removeWSpacesBWWords function takes two formal arguments. They are input string( inputStr) and Output String( outputStr). This function removes extra white spaces between the words in the input string and stores the result in the Output string( outputStr).

Here is the program with a user-defined function to remove extra spaces in btw words

We need to call the removeWSpacesBWWords() function from the main() function with the two strings. i.e inputStr and outputStr.

Finally, Print the results on the screen using printffunction.

Program Output:

Let’s compile and run the program and observe the output.

trim-extra-spaces-between-words-using-functions

As we can see from the above input and output, The program is properly removing the unnecessary whitespace in between the words in the given string/sentence.

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

4 Responses

  1. […] Remove extra whitespaces in between words in a String/Sentence. […]

  2. […] C Program to Remove Extra Spaces between the Words in String […]

  3. […] C Program to Remove Extra Spaces between the Words in String […]

  4. […] C Program to Remove Extra Spaces between the Words in String […]

Leave a Reply