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++
// CPP program to convert given sentence
/// to camel case.
#include <bits/stdc++.h>
usingnamespacestd;
// Function to remove spaces and convert
// into camel case
string convert(string s)
{
intn = s.length();
intres_ind = 0;
for(inti = 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
returns.substr(0, res_ind);
}
// Driver program
intmain()
{
string str = "I get intern at geeksforgeeks";
cout << convert(str);
return0;
}
Java
// Java program to convert given sentence
/// to camel case.
classGFG
{
// Function to remove spaces and convert
// into camel case
staticString convert(String s)
{
// to count spaces
intcnt= 0;
intn = s.length();
charch[] = s.toCharArray();
intres_ind = 0;
for(inti = 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
returnString.valueOf(ch, 0, n - cnt);
}
// Driver code
publicstaticvoidmain(String args[])
{
String str = "I get intern at geeksforgeeks";
System.out.println(convert(str));
}
}
// This code is contributed by gp6.
Python
# Python program to convert
# given sentence to camel case.
# Function to remove spaces
# and convert into camel case
defconvert(s):
if(len(s) ==0):
return
s1 =''
s1 +=s[0].upper()
fori inrange(1, len(s)):
if(s[i] ==' '):
s1 +=s[i +1].upper()
i +=1
elif(s[i -1] !=' '):
s1 +=s[i]
print(s1)
# Driver Code
defmain():
s ="I get intern at geeksforgeeks"
convert(s)
if__name__=="__main__":
main()
# This code is contributed
# prabhat kumar singh
C#
// C# program to convert given sentence
// to camel case.
usingSystem;
classGFG
{
// Function to remove spaces and convert
// into camel case
staticvoidconvert(String s)
{
// to count spaces
intcnt= 0;
intn = s.Length;
char[]ch = s.ToCharArray();
intres_ind = 0;
for(inti = 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(inti = 0; i < n - cnt; i++)
Console.Write(ch[i]);
}
// Driver code
publicstaticvoidMain(String []args)
{
String str = "I get intern at geeksforgeeks";
convert(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
<script>
// Function to remove spaces and convert
// into camel case
functionconvert( s)
{
varn = s.length;
varstr="";
for(vari = 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
returnstr;
}
varstr = "I get intern at geeksforgeeks";
document.write(convert(str));
</script>
Output
IGetInternAtGeeksforgeeks
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy