Camel case of a given sentence - GeeksforGeeks

09 May, 2022
Given a sentence, task is to remove spaces from the sentence and rewrite in Camel case. It is a style of writing where we don’t have spaces and all words begin with capital letters.Examples:  
Input  : I got intern at geeksforgeeks
Output : IGotInternAtGeeksforgeeks

Input :  Here comes the garden
Output : HereComesTheGarden
Simple solution: First method is to traverse sentence and one by one remove spaces by moving subsequent characters one position back and changing case of first character to capital. It takes O(n*n) time.Efficient solution : We traverse given string, while traversing we copy non space character to result and whenever we encounter space, we ignore it and change next letter to capital.Below is code implementation  
C++
Java
Python
C#
Javascript
// CPP program to convert given sentence/// to camel case.#include <bits/stdc++.h>using namespace std; // Function to remove spaces and convert// into camel casestring convert(string s){    int n = s.length();     int res_ind = 0;     for (int i = 0; i < n; i++) {         // check for spaces in the sentence        if (s[i] == ' ') {             // conversion into upper case            s[i + 1] = toupper(s[i + 1]);            continue;        }         // If not space, copy character        else            s[res_ind++] = s[i];            }     // return string to main    return s.substr(0, res_ind);} // Driver programint main(){    string str = "I get intern at geeksforgeeks";    cout << convert(str);    return 0;}
// Java program to convert given sentence/// to camel case.class GFG{     // Function to remove spaces and convert    // into camel case    static String convert(String s)    {         // to count spaces        int cnt= 0;        int n = s.length();        char ch[] = s.toCharArray();        int res_ind = 0;         for (int i = 0; i < n; i++)        {             // check for spaces in the sentence            if (ch[i] == ' ')            {                cnt++;                // conversion into upper case                ch[i + 1] = Character.toUpperCase(ch[i + 1]);                continue;            }             // If not space, copy character            else                ch[res_ind++] = ch[i];        }         // new string will be reduced by the        // size of spaces in the original string        return String.valueOf(ch, 0, n - cnt);    }     // Driver code    public static void main(String args[])    {        String str = "I get intern at geeksforgeeks";        System.out.println(convert(str));    }} // This code is contributed by gp6.
# Python program to convert# given sentence to camel case. # Function to remove spaces# and convert into camel casedef convert(s):    if(len(s) == 0):        return    s1 = ''    s1 += s[0].upper()    for i in range(1, len(s)):        if (s[i] == ' '):            s1 += s[i + 1].upper()            i += 1        elif(s[i - 1] != ' '):            s1 += s[i]    print(s1)                  # Driver Codedef main():    s = "I get intern at geeksforgeeks"    convert(s)     if __name__=="__main__":    main()     # This code is contributed# prabhat kumar singh    
// C# program to convert given sentence// to camel case.using System;     class GFG{     // Function to remove spaces and convert    // into camel case    static void convert(String s)    {         // to count spaces        int cnt= 0;        int n = s.Length;        char []ch = s.ToCharArray();        int res_ind = 0;         for (int i = 0; i < n; i++)        {             // check for spaces in the sentence            if (ch[i] == ' ')            {                cnt++;                                 // conversion into upper case                ch[i + 1] = char.ToUpper(ch[i + 1]);                continue;            }             // If not space, copy character            else                ch[res_ind++] = ch[i];        }         // new string will be reduced by the        // size of spaces in the original string        for(int i = 0; i < n - cnt; i++)            Console.Write(ch[i]);    }     // Driver code    public static void Main(String []args)    {        String str = "I get intern at geeksforgeeks";        convert(str);    }} // This code is contributed by 29AjayKumar
<script> // Function to remove spaces and convert// into camel casefunction convert( s){    var n = s.length;     var str="";    for (var i = 0; i < n; i++)    {         // check for spaces in the sentence        if (s[i] == ' ')        {            // conversion into upper case            str+= s[i+1].toUpperCase();            i++;                     }         // If not space, copy character        else{                     str+= s[i];            }    }     // return string to main    return str;} var str = "I get intern at geeksforgeeks";    document.write(convert(str));  </script>
IGetInternAtGeeksforgeeks
 
YouTubeGeeksforGeeks507K subscribersCamel 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 / 3:20•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=okegGKuR5RI" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
prabhat kumar singh
gp6
29AjayKumar
sgrpwr
akshitsaxenaa09
simmytarika5
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters
Reverse words in a given string
Remove duplicates from a given string
How to split a string in C/C++, Python and Java?
Reverse string in Python (5 different ways)