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:
- Traverse the whole string character by character.
- If the character is a space, replace it with the hyphen ‘-‘.
Below is the implementation of the above approach:
C++
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
string str = "A computer science portal for geeks" ;
for ( int i = 0; i < str.length(); ++i) {
if (str[i] == ' ' ) {
str[i] = '-' ;
}
}
cout << str << endl;
return 0;
}
|
Java
class GFG
{
static String replaceSpace(String str)
{
String s = "" ;
for ( int i = 0 ; i < str.length(); ++i) {
if (str.charAt(i) == ' ' )
s += '-' ;
else
s += str.charAt(i);
}
return s;
}
public static void main(String []args)
{
String str = "A computer science portal for geeks" ;
System.out.println(replaceSpace(str));
}
}
|
Python3
if __name__ = = '__main__' :
str = "A computer science portal for geeks"
for i in range ( 0 , len ( str ), 1 ):
if ( str [i] = = ' ' ):
str = str .replace( str [i], '-' )
print ( str )
|
C#
using System;
public class GFG
{
static String replaceSpace(String str)
{
String s = "" ;
for ( int i = 0; i < str.Length; ++i) {
if (str[i] == ' ' )
s += '-' ;
else
s += str[i];
}
return s;
}
public static void Main()
{
String str = "A computer science portal for geeks" ;
Console.Write(replaceSpace(str));
}
}
|
PHP
<?php
$str = "A computer science portal for geeks" ;
for ( $i = 0; $i < strlen ( $str ); ++ $i )
{
if ( $str [ $i ] == ' ' )
{
$str [ $i ] = '-' ;
}
}
echo $str . "\n" ;
|
Javascript
<script>
var str = "A computer science portal for geeks" ;
var newStr = str.split( "" );
for ( var i = 0; i < newStr.length; ++i) {
if (newStr[i] === " " ) {
newStr[i] = "-" ;
}
}
document.write(newStr.join( "" ) + "<br>" );
</script>
|
Output
A-computer-science-portal-for-geeks
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Method #2: Using built-in methods :
- As all the words in a sentence are separated by spaces.
- We have to split the sentence by spaces using built-in methods
- We split all the words by spaces and store them in a list.
- Join the list using ‘-‘ and store in a string
- Print the string
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
string printHyphen(string str)
{
for ( int i=0; i<str.length(); i++)
{
if (str[i] == ' ' )
str[i] = '-' ;
}
return str;
}
int main()
{
string str = "Text contains malayalam and level words" ;
cout << printHyphen(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String printHyphen(String string)
{
string = string.replace( ' ' , '-' );
return string;
}
public static void main(String[] args)
{
String string = "Text contains malayalam and level words" ;
System.out.print(printHyphen(string));
}
}
|
Python3
def printHyphen(string):
lis = list (string.split( " " ))
string = '-' .join(lis)
return string
string = "Text contains malayalam and level words"
print (printHyphen(string))
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class HelloWorld {
public static string printHyphen( string s)
{
string [] str = s.Split( ' ' );
string res = string .Join( "-" , str);
return res;
}
static void Main() {
string str = "Text contains malayalam and level words" ;
Console.WriteLine(printHyphen(str));
}
}
|
Javascript
<script>
function printHyphen(string){
var lis = string.split( " " )
string = lis.join( "-" );
return string;
}
var string = "Text contains malayalam and level words" ;
document.write(printHyphen(string));
</script>
|
Output
Text-contains-malayalam-and-level-words
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...