Home » Program to Replace the Spaces of a String with a Specific Character

Program to Replace the Spaces of a String with a Specific Character

by Online Tutorials Library

Program to replace the spaces of a string with a specific character

Explanation

In this program, we need to replace all the spaces present in the string with a specific character.

One of the approach to accomplish this is by iterating through the string to find spaces. If spaces are present, then assign specific character in that index. Other approach is to use a built-in function replace function to replace space with a specific character.

Algorithm

  1. Define a string.
  2. Determine the character ‘ch’ through which spaces need to be replaced.
  3. Use replace function to replace space with ‘ch’ character.

Solution

Python

Output:

String after replacing spaces with given character:   Once-in-a-blue-moon  

C

Output:

String after replacing spaces with given character:   Once-in-a-blue-moon  

JAVA

Output:

String after replacing spaces with given character:   Once-in-a-blue-moon  

C#

Output:

String after replacing spaces with given character:   Once-in-a-blue-moon  

PHP

Output:

String after replacing spaces with given character:   Once-in-a-blue-moon  

Next Topic#

You may also like