Going national

  • 10
  • Locked
Once diners get over their initial reservations about the proper color of breakfast foods, they fall in love with green eggs and ham. Sam-I-Am has big plans to open a national chain of restaurants featuring his signature dish. He'll launch the Green Eggs and Ham restaurant chain as soon as he can find suppliers of properly colored eggs. We will write a program that replaces the text "green eggs and ham" with "Green Eggs And Ham".
You can't complete this task, because you're not signed in.
Comments (35)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 16 , Italy
9 March, 14:37
It is a good task to learn some string methods. However, I think the settings are ill-defined. In my opinion, the following conditions should be specified in the settings, not after checking our code: - strings may begin with whitespace; - words may be separated by more than one space.
Level 19 , Kyiv, Ukraine
13 July 2022, 08:14
I break string into char array. Loop. If char equals to whitespace and next char dosn't equal to whitespace than replace next char with upperCase the same char. In the end I return to String, although you can print it thru loop of char array.
Level 10 , Brazil
18 April 2022, 17:55
Hint: Array[].split() don't stores space between words. If theres more than one space between words, the program should put it.
Level 21 , United States
22 January 2022, 08:29
Why can't we do this instead of turning the string to a char array?
String[] split = s.split(" ");

        for (int i = 0; i < split.length; i++) {

            char firstLetter = split[i].charAt(0); //get the first letter
            char toUpperCase = Character.toUpperCase(firstLetter); //set the first letter to upperCase

            split[i] = split[i].replace(firstLetter, toUpperCase);
            System.out.print((i == split.length - 1)? split[i] : split[i] + " ");
Gellert Varga
Level 23 , Szekesfehervar, Hungary
20 May 2022, 20:00
Level 19 , Kyiv, Ukraine
13 July 2022, 08:06
In this case what will happen if you will have word with another letter like first one, e.g. "sams" ? You'll get "SamS".
Anonymous #10775689
Level 14 , United Kingdom
1 August 2021, 11:27
Anonymous #11074513
Level 10 , Taiwan, Province of China
22 September 2022, 00:49
Justin Smith
Level 39 , Greenfield, USA, United States
15 July 2021, 18:56
This one is only hard in the sense that you have to use String methods that we haven't used or seen before. Google is your friend. Think about what you need to do with the string, and if you ask the right question Google will display the answer. Google (or other search tool) is an essential tool for any programming language. Nobody expects a programmer to memorize every single method or function in existence. You'll have to get used to looking things up.
Adam Odoj
Level 22 , Poland
29 November 2021, 14:31
That´s very interesting advice. I was learning in old school mode to try to memorize what I can. THANKS!!!
Level 41 , Olympus Mons, Mars
28 May 2021, 06:56
So if any of you were confused by the lackluster instructions on this one, here are some additional parameters that might help you: - You need to account for if the user input has more than one space inbetween the words. - When you print out the text, you only need one space between words, so you can eliminate the extra spaces. (You don't need to print out the extra spaces exactly like the input, which I did, and in fact won't pass) Some tips: - Use split(). - Then trim() the spaces. - Learn about substring() or convert the trimmed array into individual char arrays and toUpperCase() on the first letter. - And (credit to Steve, somewhere in the comments) split by "\\s+". Which notates splitting by one or more white spaces. If you want to look this up, its a regex expression.
Level 15 , Old Town, United States
3 May 2021, 02:23
As someone previously said, another forced self-learning, but this one is pretty ridiculous in its scope (stackOverFlow suggests 20+ ways to solve it & only understand parts of each solutions). Got it to pass, but gonna have to some more time studying these methods and their logic. Fooyee!!
ayhem bouabid
Level 25 , Tunis, Tunisia
7 January 2021, 08:57
Well, I have seen many comments about changing the string itself, but I think such a choice is not that favorable after all. For me, I decided to make a different string variable: resultSentence. then I wrote a method that returns the indices of the first letters in each word in the sentence aka the indices that come right after the space character. Using the return list, I can know which of the characters need to be uppercase and which are lowercase.
Karas Java Developer
23 October 2020, 00:41
Yes these are your best friends for this task: .split() .charAt() .substring() and use just System.out.print instead of println, depending on your code. As an additional note, make sure to test your answer with a string that has 2 or more spaces. That one got me spining for some minutes.
Level 15 , Old Town, United States
3 May 2021, 02:48
and .trim()