GeeksforGeeks App
Open App
Browser
Continue

# Camel case of a given sentence

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```

Naive approach:

The first method is to traverse the sentence till our sentence becomes space free. So every time when we encounter any space we will remove that space and make the character that was next to it as capital.

So what we will do that, we will run a while loop and this while loop will terminate when we will reach the end of the string. And we can only reach end of the string when it does not contain any space. So in that while loop, we have an inner loop that will run from the starting of the string, and if it got any space then it will remove that and make the character next to the space as capital and then terminates.If no space is available in the string then it terminates on going to the end of the string.

Code-

## C++

```// CPP program to convert given sentence
/// to camel case.
#include <bits/stdc++.h>
using namespace std;

// Function to remove spaces and convert
// into camel case
string convert(string str)
{
int i=0;
while(true){
i=0;
while(i<str.size()){
if(str[i]==' '){
str[i+1] = toupper(str[i+1]);
str.erase(i,1);
//Terminate inner loop after removing a space and making character next to it as capital
break;
}
i++;
}
//Terminate outer loop when we reach to the end of string
if(i==str.size()){break;}
}

return str;
}

// Driver program
int main()
{
string str = "I get intern at geeksforgeeks";
cout << convert(str);
return 0;
}

```

## Java

```import java.util.*;

public class Main {
// Function to remove spaces and convert into camel case
public static String convert(String str) {
int i = 0;
while (true) {
i = 0;
while (i < str.length()) {
if (str.charAt(i) == ' ') {
char nextChar = Character.toUpperCase(str.charAt(i+1));
str = str.substring(0, i) + nextChar + str.substring(i+2);
// Terminate inner loop after removing a space and making character next to it as capital
break;
}
i++;
}
// Terminate outer loop when we reach to the end of string
if (i == str.length()) {
break;
}
}

return str;
}

// Driver program
public static void main(String[] args) {
String str = "I get intern at geeksforgeeks";
System.out.println(convert(str));
}
}
```

## Python3

```# Python3 program to convert given sentence
# to camel case.

# Function to remove spaces and convert
# into camel case
def convert(string):
while True:
i = 0
while i < len(string):
if string[i] == ' ':
string = string[:i] + string[i+1:].capitalize()
# Terminate inner loop after removing a space and making character next to it as capital
break
i += 1
# Terminate outer loop when we reach to the end of string
if i == len(string):
break
return string

# Driver program
if __name__ == '__main__':
string = "I get intern at geeksforgeeks"
print(convert(string))
```

## C#

```using System;
using System.Text.RegularExpressions;

public class Program {
public static void Main()
{
string str = "I get intern at geeksforgeeks";
Console.WriteLine(Convert(str));
}

public static string Convert(string str)
{
int i = 0;
while (true) {
i = 0;
while (i < str.Length) {
// Check if current character is a space
if (str[i] == ' ') {
// Capitalize next character
char nextChar
= char.ToUpper(str[i + 1]);
// Update string
str = str.Substring(0, i) + nextChar
+ str.Substring(i + 2);
break;
}
i++;
}
if (i == str.Length) {
break;
}
}
return str;
}
}```

## Javascript

```// JS program to convert given sentence
/// to camel case.

// Function to remove spaces and convert
// into camel case
function convert(str) {
let i = 0;
while (true) {
i = 0;
while (i < str.length) {
if (str[i] == ' ') {
let charArray = [...str];
charArray[i + 1] = charArray[i + 1].toUpperCase();
str = "";
for (let k = 0; k < charArray.length; k++) {
if (k != i)
str += charArray[k];
}
//Terminate inner loop after removing a space and making character next to it as capital
break;
}
i++;
}
//Terminate outer loop when we reach to the end of string
if (i == str.length) { break; }
}

return str;
}

// Driver program
let str = "I get intern at geeksforgeeks";
console.log(convert(str));

// This code is contributed by akashish__
```
Output

`IGetInternAtGeeksforgeeks`

Time Complexity: O(n*n)
Auxillary Space: O(1)

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>
using namespace std;

// Function to remove spaces and convert
// into camel case
string 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 program
int main()
{
string str = "I get intern at geeksforgeeks";
cout << convert(str);
return 0;
}

```

## Java

```// 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

```# Python program to convert
# given sentence to camel case.

# Function to remove spaces
# and convert into camel case
def 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 Code
def main():
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.
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

```

## Javascript

```<script>

// Function to remove spaces and convert
// into camel case
function 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>```
Output

`IGetInternAtGeeksforgeeks`

Time complexity: O(n) //since one traversal of the string is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) //since no extra array is used, the space taken by the algorithm is constant

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.

My Personal Notes arrow_drop_up