A simple solution is to the convert whole string into upper letter and then traverse the given string, while traversing we replace the initial of all words to small.

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to covert a

// sentence to gOOGLE cASE.

classGFG

{

staticString convert(String str)

{

// empty strings

String w = "", z = "";

// convert input string to upper case

str = str.toUpperCase() + " ";

for(inti = 0; i < str.length(); i++)

{

// checki if character is not a space

// and adding it to string w

charch = str.charAt(i);

if(ch != ' ')

w = w + ch;

else{

// converting first character to lower

// case and subsequent initial

// letter of another word to lower case

z = z + (Character.toLowerCase(w.charAt(0))) +

w.substring(1) + " ";

w = "";

}

}

returnz;

}

// Driver code

publicstaticvoidmain(String[] args)

{

String str = "I got intern at geeksforgeeks";

System.out.println(convert(str));

}

}

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to covert a

// sentence to gOOGLE cASE.

usingSystem;

classGFG

{

staticstringconvert(stringstr)

{

// empty strings

stringw = "", z = "";

// convert input string

// to upper case

str = str.ToUpper() + " ";

for(inti = 0;

i < str.Length; i++)

{

// checki if character is

// not a space and adding

// it to string w

charch = str[i];

if(ch != ' ')

w = w + ch;

else

{

// converting first character

// to lower case and subsequent

// initial letter of another

// word to lower case

z = z + (Char.ToLower(w[0])) +

w.Substring(1) + " ";

w = "";

}

}

returnz;

}

// Driver code

staticvoidMain()

{

stringstr = "I got intern at geeksforgeeks";

Console.WriteLine(convert(str));

}

}

// This code is contributed by

// Manish Shaw(manishshaw1)

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP program to covert a

// sentence to gOOGLE cASE.

functionconvert($str)

{

// empty strings

$w= ""; $z= "";

// convert input

// to upper case

$str= strtoupper($str) . " ";

for($i= 0;

$i< strlen($str); $i++)

{

// checki if character

// is not a space

// and adding it to $w

$ch= $str[$i];

if($ch!= ' ')

$w= $w. $ch;

else

{

// converting first character

// to lower case and subsequent

// initial letter of another

// word to lower case

$z= $z. strtolower($w[0]) .

substr($w, 1) . " ";

$w= "";

}

}

return$z;

}

// Driver code

$str= "I got intern at geeksforgeeks";

echo(convert($str));

// This code is contributed by

// Manish Shaw(manishshaw1)

?>

chevron_right

filter_none

Output :

i gOT iNTERN aT gEEKSFORGEEKS

The above solution requires two traversals of string. An efficient solution is to do in single traversal. The idea is to keep track of spaces. After every space, print character to lower, else print in upper.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.