Snake Case | Practice | GeeksforGeeks

Given a Sentence S of length N containing only english alphabet characters, your task is to write a program that converts the given sentence to Snake Case sentence. Snake case is the practice of writing compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, and the first letter of each word written in lowercase. For ease keep all the characters in lowercase.
Note: The given sentence will not start with a Whitespace.
Example 1: 
Input: 
N = 14
S = "Geeks ForGeeks"
Output: "geeks_forgeeks"
Explanation: All upper case characters are
converted to lower case and the whitespace
characters are replaced with underscore '_'.
Example 2: 
Input: 
N = 21
S = "Here comes the garden"
Output: "here_comes_the_garden"
Explanation: All upper case characters are
converted to lower case and the whitespace 
characters are replaced with underscore '_'.
Your Task:
You don't need to read input or print anything. Your task is to complete the function snakeCase() which takes N and a String S as input parameters and returns the converted string .
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
0
vishal8579941 week ago
 // Very Easy Solutions of Java//User function Template for Java
class Solution {
   static String snakeCase(String S , int n) {
       // code here
       StringBuilder sb=new StringBuilder();
       for(int i=0;i<n;i++){
           char ch=S.charAt(i);
           if(ch>='A' && ch<='Z'){
               char c=(char)('a'+(ch-'A'));
               sb.append(c);
           }else if(ch==' '){
               sb.append('_');
           }else{
               sb.append(ch);
           }
       }
       return sb.toString();
   }
};
0
manjeetschan12341 week ago
//c++
 string snakeCase(string S , int n) {       // code here       string a="";            for(int i=0;i<S.size();i++){                                if(S[i]>='A'&&S[i]<='Z'){                   a+=S[i]+32;               }               if(S[i]>='a'&&S[i]<='z'){                   a+=S[i];               }               if(S[i]==' '){                   a+='_';               }                  }       return a;   }
0
arpitsharma202 weeks ago
JAVA
class Solution {   static String snakeCase(String S , int n) {       // code here       return S.toLowerCase().replaceAll(" ", "_");   }};
0
mrdiaz56563 weeks ago
class Solution {   static String snakeCase(String S , int n)    {       String res=S.toLowerCase();              StringBuilder str= new StringBuilder(res);        for(int i=0;i<S.length();i++){       if(res.charAt(i)==' ')       {           str.setCharAt(i,'_');       }       }       return str.toString();   }};
0
codewithshoaib191 month ago
S=S.toLowerCase();      S= S.replaceAll(" ","_");       return S;
0
ravuripraneeth25001 month ago
class Solution {
 public:
   string snakeCase(string s , int n) {
       transform(s.begin(), s.end(), s.begin(), ::tolower);
       string b;
       
       for(int i=0; i<s.size(); i++){
           if(s[i] == ' '){
               b.push_back('_');
           }
           else{
               b.push_back(s[i]);
           }
       }
       
       return b;
   }
};

+1
imohdalam1 month ago
Java | O(n)
class Solution {
    static String snakeCase(String S , int n) {
        
        StringBuilder ans = new StringBuilder();
        
        for(int i=0; i<n; i++){
            char ch = S.charAt(i);
            if(ch >= 65 && ch <= 90){
                ch = (char)(ch + 32);
            }else if( ch == ' '){
                ch = '_';
            }
            ans.append(ch);
        }
        return ans.toString();
    }
};
0
saikirannagarjuna0072 months ago
class Solution {   static String snakeCase(String S , int n) {       // code here       S=S.toLowerCase();       String b=S.replaceAll(" ","_");       return b;   }};
0
imabheek2 months ago
// Java O(n) 4 liner solution 
char [] arr = S.toLowerCase().toCharArray();       for(int i = 0; i < n; i++)             if(arr[i]==' ') arr[i] = '_';       return new String(arr);
0
imabheek2 months ago
//Java One liner O(1)
return S.replace(' ','_').toLowerCase();
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.