📜  给定句子的蛇格

📅  最后修改于: 2022-05-13 01:57:08.714000             🧑  作者: Mango

给定句子的蛇格

给定一个句子,任务是从句子中删除空格并在 Snake case 中重写。这是一种写作风格,我们用下划线替换空格,所有单词都以小写字母开头。
例子 :

Input :  I got intern at geeksforgeeks
Output : i_got_intern_at_geeksforgeeks

Input : Here comes the garden
Output : here_comes_the_garden

简单的解决方案:第一种方法是遍历句子,用下划线逐个替换空格,并将第一个字符的大小写更改为小写字母。它需要O(n*n)时间。
有效的解决方案:我们遍历给定的字符串,在遍历时我们用下划线替换空格字符,每当遇到非空格字母时,我们将该字母更改为小。
下面是代码实现:

C++
// CPP program to convert given sentence
/// to snake case
#include 
using namespace std;
 
// Function to replace spaces and convert
// into snake case
void convert(string str)
{
    int n = str.length();
 
    for (int i = 0; i < n; i++)
    {
        // Converting space to underscore
        if (str.at(i) == ' ')
            str.at(i) = '_';
        else
            // If not space, convert into lower character
            str.at(i) = tolower(str.at(i));
    }
     
    cout << str;
}
 
// Driver program
int main()
{
    string str = "I got intern at geeksforgeeks";
    // Calling function
    convert(str);
    return 0;
}


Java
// Java program to convert
// given sentence to
// snake case
import java.io.*;
 
class GFG
{
     
// Function to replace
// spaces and convert
// into snake case
static void convert(String str)
{
    int n = str.length();
    String str1 = "";
 
    for (int i = 0; i < n; i++)
    {
        // Converting space
        // to underscore
        if (str.charAt(i) == ' ')
            str1 = str1 + '_';
        else
         
            // If not space, convert
            // into lower character
            str1 = str1 +
                   Character.toLowerCase(str.charAt(i));
    }
     
    System.out.print(str1);
}    
 
// Driver Code
public static void main(String args[])
{
    String str = "I got intern at geeksforgeeks";
     
    // Calling function
    convert(str);
}
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Python3
# Python3 program to convert given sentence
# to snake case
 
# Function to replace spaces and convert
# into snake case
def convert(string) :
 
    n = len(string);
    string = list(string);
     
    for i in range(n) :
     
        # Converting space to underscore
        if (string[i] == ' ') :
            string[i] = '_';
        else :
            # If not space, convert
            # into lower character
            string[i] = string[i].lower();
     
    string = "".join(string)
    print(string);
 
# Driver program
if __name__ == "__main__" :
 
    string = "I got intern at geeksforgeeks";
     
    # Calling function
    convert(string);
 
# This code is contributed by AnkitRai01


C#
// C# program to convert
// given sentence to
// snake case
using System;
 
class GFG
{
    // Function to replace
    // spaces and convert
    // into snake case
    static void convert(string str)
    {
        int n = str.Length;
        string str1 = "";
     
        for (int i = 0; i < n; i++)
        {
            // Converting space
            // to underscore
            if (str[i] == ' ')
                str1 = str1 + '_';
            else
             
                // If not space, convert
                // into lower character
                str1 = str1 +
                       Char.ToLower(str[i]);
        }        
        Console.Write(str1);
    }    
     
    // Driver Code
    static void Main()
    {
        string str = "I got intern at geeksforgeeks";
         
        // Calling function
        convert(str);
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP


Javascript


输出 :

i_got_intern_at_geeksforgeeks