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++
#include <bits/stdc++.h>
using namespace std;
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);
break ;
}
i++;
}
if (i==str.size()){ break ;}
}
return str;
}
int main()
{
string str = "I get intern at geeksforgeeks" ;
cout << convert(str);
return 0;
}
|
Java
import java.util.*;
public class Main {
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 );
break ;
}
i++;
}
if (i == str.length()) {
break ;
}
}
return str;
}
public static void main(String[] args) {
String str = "I get intern at geeksforgeeks" ;
System.out.println(convert(str));
}
}
|
Python3
def convert(string):
while True :
i = 0
while i < len (string):
if string[i] = = ' ' :
string = string[:i] + string[i + 1 :].capitalize()
break
i + = 1
if i = = len (string):
break
return string
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) {
if (str[i] == ' ' ) {
char nextChar
= char .ToUpper(str[i + 1]);
str = str.Substring(0, i) + nextChar
+ str.Substring(i + 2);
break ;
}
i++;
}
if (i == str.Length) {
break ;
}
}
return str;
}
}
|
Javascript
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];
}
break ;
}
i++;
}
if (i == str.length) { break ; }
}
return str;
}
let str = "I get intern at geeksforgeeks" ;
console.log(convert(str));
|
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++
#include <bits/stdc++.h>
using namespace std;
string convert(string s)
{
int n = s.length();
int res_ind = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == ' ' ) {
s[i + 1] = toupper (s[i + 1]);
continue ;
}
else
s[res_ind++] = s[i];
}
return s.substr(0, res_ind);
}
int main()
{
string str = "I get intern at geeksforgeeks" ;
cout << convert(str);
return 0;
}
|
Java
class GFG
{
static String convert(String s)
{
int cnt= 0 ;
int n = s.length();
char ch[] = s.toCharArray();
int res_ind = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (ch[i] == ' ' )
{
cnt++;
ch[i + 1 ] = Character.toUpperCase(ch[i + 1 ]);
continue ;
}
else
ch[res_ind++] = ch[i];
}
return String.valueOf(ch, 0 , n - cnt);
}
public static void main(String args[])
{
String str = "I get intern at geeksforgeeks" ;
System.out.println(convert(str));
}
}
|
Python
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)
def main():
s = "I get intern at geeksforgeeks"
convert(s)
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static void convert(String s)
{
int cnt= 0;
int n = s.Length;
char []ch = s.ToCharArray();
int res_ind = 0;
for ( int i = 0; i < n; i++)
{
if (ch[i] == ' ' )
{
cnt++;
ch[i + 1] = char .ToUpper(ch[i + 1]);
continue ;
}
else
ch[res_ind++] = ch[i];
}
for ( int i = 0; i < n - cnt; i++)
Console.Write(ch[i]);
}
public static void Main(String []args)
{
String str = "I get intern at geeksforgeeks" ;
convert(str);
}
}
|
Javascript
<script>
function convert( s)
{
var n = s.length;
var str= "" ;
for ( var i = 0; i < n; i++)
{
if (s[i] == ' ' )
{
str+= s[i+1].toUpperCase();
i++;
}
else {
str+= s[i];
}
}
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
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Apr, 2023
Like Article
Save Article