Snake case of a given sentence

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to convert given sentence
/// to snake case
#include <bits/stdc++.h>
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 underscor
        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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 underscor
        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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 underscor
            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)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to convert given 
// sentence to snake case
  
// Function to replace spaces 
// and convert into snake case
function convert($str)
{
    $n = strlen($str);
  
    for ($i = 0; $i < $n; $i++)
    {
        // Converting space
        // to underscor
        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 function
convert($str);
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)
?>

chevron_right



Output :

i_got_intern_at_geeksforgeeks


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.



Improved By : manishshaw1, Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.