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++
#include <bits/stdc++.h>
using namespace std;
void convert(string str)
{
int n = str.length();
for ( int i = 0; i < n; i++)
{
if (str.at(i) == ' ' )
str.at(i) = '_' ;
else
str.at(i) = tolower (str.at(i));
}
cout << str;
}
int main()
{
string str = "I got intern at geeksforgeeks" ;
convert(str);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void convert(String str)
{
int n = str.length();
String str1 = "" ;
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) == ' ' )
str1 = str1 + '_' ;
else
str1 = str1 +
Character.toLowerCase(str.charAt(i));
}
System.out.print(str1);
}
public static void main(String args[])
{
String str = "I got intern at geeksforgeeks" ;
convert(str);
}
}
|
Python3
def convert(string) :
n = len (string);
string = list (string);
for i in range (n) :
if (string[i] = = ' ' ) :
string[i] = '_' ;
else :
string[i] = string[i].lower();
string = "".join(string)
print (string);
if __name__ = = "__main__" :
string = "I got intern at geeksforgeeks" ;
convert(string);
|
C#
using System;
class GFG
{
static void convert( string str)
{
int n = str.Length;
string str1 = "" ;
for ( int i = 0; i < n; i++)
{
if (str[i] == ' ' )
str1 = str1 + '_' ;
else
str1 = str1 +
Char.ToLower(str[i]);
}
Console.Write(str1);
}
static void Main()
{
string str = "I got intern at geeksforgeeks" ;
convert(str);
}
}
|
PHP
<?php
function convert( $str )
{
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == ' ' )
$str [ $i ] = '_' ;
else
$str [ $i ] = strtolower ( $str [ $i ]);
}
echo $str ;
}
$str = "I got intern at geeksforgeeks" ;
convert( $str );
?>
|
Javascript
<script>
function convert (str) {
let n = str.length;
let str1 = "" ;
for ( i = 0; i < n; i++)
{
if (str[i] == ' ' )
str1 = str1 + '_' ;
else
str1 = str1 + (str[i]).toLowerCase();
}
document.write(str1);
}
let str = "I got letern at geeksforgeeks" ;
convert(str);
</script>
|
Output
i_got_intern_at_geeksforgeeks
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Sep, 2022
Like Article
Save Article