Snake case of a given sentence - GeeksforGeeks

27 Dec, 2021
Given a sentence, task is to remove spaces from the sentence and rewrite in Snake case. It is a style of writing where we replace spaces with underscore and all words begin with small letters.Examples :  
Input :  I got intern at geeksforgeeks
Output : i_got_intern_at_geeksforgeeks

Input : Here comes the garden
Output : here_comes_the_garden
 
Simple solution : First method is to traverse sentence and one by one replace spaces by underscores and changing case of first character to small letter. It takes O(n*n) time.Efficient solution : We traverse given string, while traversing we replace space character with underscore and whenever we encounter non-space letter, we change that letter to small.Below is the code implementation :  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to convert given sentence/// to snake case#include <bits/stdc++.h>using namespace std; // Function to replace spaces and convert// into snake casevoid 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 programint main(){    string str = "I got intern at geeksforgeeks";    // Calling function    convert(str);    return 0;}
// Java program to convert// given sentence to// snake caseimport java.io.*; class GFG{     // Function to replace// spaces and convert// into snake casestatic 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 Codepublic 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 program to convert given sentence# to snake case # Function to replace spaces and convert# into snake casedef 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 programif __name__ == "__main__" :     string = "I got intern at geeksforgeeks";         # Calling function    convert(string); # This code is contributed by AnkitRai01
// C# program to convert// given sentence to// snake caseusing 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// PHP program to convert given// sentence to snake case // Function to replace spaces// and convert into snake casefunction convert($str){    $n = strlen($str);     for ($i = 0; $i < $n; $i++)    {        // Converting space        // to underscore        if ($str[$i] == ' ')            $str[$i] = '_';        else            // If not space, convert            // into lower character            $str[$i] = strtolower($str[$i]);    }    echo $str;} // Driver Code$str = "I got intern at geeksforgeeks"; // Calling functionconvert($str); // This code is contributed// by Akanksha Rai(Abby_akku)?>
<script>// Javascript program to convert// given sentence to// snake case     // Function to replace    // spaces and convert    // into snake case    function convert (str) {        let n = str.length;        let str1 = "";         for ( i = 0; i < n; i++)        {                     // Converting space            // to underscore            if (str[i] == ' ')                str1 = str1 + '_';            else                 // If not space, convert                // into lower letacter                str1 = str1 + (str[i]).toLowerCase();        }         document.write(str1);    }     // Driver Code    let str = "I got letern at geeksforgeeks";     // Calling function    convert(str); // This code is contributed by gauravrajput1</script>
Output :  
i_got_intern_at_geeksforgeeks
 
YouTubeGeeksforGeeks500K subscribersSnake case of a given sentence | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:48•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=DB1Cj72963Q" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
manishshaw1
Akanksha_Rai
ankthon
GauravRajput1
khushboogoyal499
varshagumber28
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 String Coding Problems for Interviews
Hill Cipher
Vigenère Cipher
Naive algorithm for Pattern Searching
Convert character array to string in C++
Reverse words in a given String in Python
How to Append a Character to a String in C
sprintf() in C
String class in Java | Set 1
Print all the duplicates in the input string