📜  程序用连字符替换字符串的每个空格

📅  最后修改于: 2021-06-01 00:29:20             🧑  作者: Mango

给定一个字符串,任务是用连字符’-‘替换单词之间的所有空格。
例子:

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

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

方法:

  1. 横越字符整个字符串的字符。
  2. 如果字符是空格,请用连字符’-‘代替。

下面是上述方法的实现:

C++
// C++ program to replace space with -
 
#include 
#include 
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;
}


Java
// 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));
     
     
    }
 
}


Python3
# 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


C#
// 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


PHP


Python3
# Python program for the above approach
 
def printHyphen(string):
 
    # Split by space and converting
    # String to list and
    lis = list(string.split(" "))
 
    # joining the list and storing in string
    string = '-'.join(lis)
    # returning thee string
    return string
 
 
# Driver code
string = "Text contains malayalam and level words"
print(printHyphen(string))
 
# This code is contributed by vikkycirus


输出:
A-computer-science-portal-for-geeks

时间复杂度: O(N)

方法2:在Python使用join():

  • 因为句子中的所有单词都用空格分隔。
  • 我们必须使用split()将句子分隔为空格
  • 我们用空格将所有单词分开,并将它们存储在列表中。
  • 使用“-”加入列表并存储在字符串
  • 打印字符串

下面是上述方法的实现:

Python3

# Python program for the above approach
 
def printHyphen(string):
 
    # Split by space and converting
    # String to list and
    lis = list(string.split(" "))
 
    # joining the list and storing in string
    string = '-'.join(lis)
    # returning thee string
    return string
 
 
# Driver code
string = "Text contains malayalam and level words"
print(printHyphen(string))
 
# This code is contributed by vikkycirus

输出:

Text-contains-malayalam-and-level-words
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”