Javatpoint Logo
Javatpoint Logo

Q. Program to remove all the white spaces from a string.

In this program, our task is to remove all the white-spaces from the string. For this purpose, we need to traverse the string and check if any character of the string is matched with a white-space character or not. If so, Use any built-in method like replace() with a blank.

In C, we do not have any built-in method to replace. Therefore, We need to run the for loop to traverse the string and see if there is any white-space character or not. If so, then start the inner loop (j) from ith character to len and keep replacing each element with its next adjacent element. Decrease the length of the string by 1 on the termination of this loop. Repeat this process until all the white-spaces of the string are removed.

Algorithm

  1. Define a string.
  2. Use replace () function to replace all the space characters with a blank.
  3. The resulting string will not have any spaces in-between them.

Solution

Python

Output:

String after removing all the white spaces : Removewhitespaces

C

Output:

String after removing all the white spaces : Removewhitespaces

JAVA

Output:

String after removing all the white spaces : Removewhitespaces

C#

Output:

String after removing all the white spaces : Removewhitespaces

PHP

Output:

String after removing all the white spaces : Removewhitespaces

Next TopicPrograms List




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials