Program to replace every space in a string with hyphen

Given a string, the task is to replace all the spaces between the words with a hyphen character ‘-‘.

Examples:

Input: str = "Geeks for Geeks."
Output: Geeks-for-Geeks.

Input: str = "A computer science portal for geeks"
Output: A-computer-science-portal-for-geeks

Approach:

  1. Traverse the whole string character by character.
  2. If the character is a space, replace it with hyphen ‘-‘.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to replace space with -
  
#include <cstring>
#include <iostream>
using namespace std;
  
int main()
{
  
    // Get the String
    string str = "A computer science portal for geeks";
  
    // Traverse the string character by character.
    for (int i = 0; i < str.length(); ++i) {
  
        // Changing the ith character
        // to '-' if it's a space.
        if (str[i] == ' ') {
  
            str[i] = '-';
        }
    }
  
    // Print the modified string.
    cout << str << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to replace space with -
  
class GFG
{
      
    // Function to replace Space with -
    static String replaceSpace(String str)
    {
          
        String s = "";
          
        // Traverse the string character by character.
        for (int i = 0; i < str.length(); ++i) {
      
            // Changing the ith character
            // to '-' if it's a space.
            if (str.charAt(i) == ' '
                s += '-';
              
            else
                s += str.charAt(i);
              
        }
      
        // return the modified string.
        return s;
      
          
    }
      
      
    public static void main(String []args)
    {
      
        // Get the String
        String str = "A computer science portal for geeks";
          
        System.out.println(replaceSpace(str));
      
      
    }
  
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to replace space with -
  
# Driver Code
if __name__ == '__main__':
      
    # Get the String
    str = "A computer science portal for geeks"
      
    # Traverse the string character by character.
    for i in range(0, len(str), 1):
          
        # Changing the ith character
        # to '-' if it's a space.
        if (str[i] == ' '):
            str = str.replace(str[i], '-')
              
    # Print the modified string.
    print(str)
      
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# program to replace space with -
using System;
public class GFG
{
       
    // Function to replace Space with -
    static String replaceSpace(String str)
    {
           
        String s = "";
           
        // Traverse the string character by character.
        for (int i = 0; i < str.Length; ++i) {
       
            // Changing the ith character
            // to '-' if it's a space.
            if (str[i] == ' '
                s += '-';
               
            else
                s += str[i];
               
        }
       
        // return the modified string.
        return s;
       
           
    }
       
       
    public static void Main()
    {
       
        // Get the String
        String str = "A computer science portal for geeks";
           
        Console.Write(replaceSpace(str));
       
       
    }
   
}
// This code is contributed by 29AjayKumar

chevron_right


PHP

Output:

A-computer-science-portal-for-geeks

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.