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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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 :