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 (31)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
RodYokoo
Level 10 , Brazil
18 April, 17:55
Hint: Array[].split() don't stores space between words. If theres more than one space between words, the program should put it.
HelloWorld
Level 20 , United States
22 January, 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, 20:00
👍
Anonymous #10775689
Level 14 , United Kingdom
1 August 2021, 11:27
Justin Smith
Level 30 , 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 15 , 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!!!
Naughtless
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.
ImDevin
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.
ImDevin
Level 15 , Old Town, United States
3 May 2021, 02:48
and .trim()
ziv fisher
Level 9 , Petah Tikva, israel
14 October 2020, 07:58
For this task you need to first Google: 1. Scanner scanner = new Scanner("string"); //take a sentence and divide it to single words. 2. Character.toUpperCase(i); // take a letter at index(i) and turn it into upperCaseLetter. 3. "string".subString(i); 4. StringBuilder - it helps to learn about it as well but not mandatory.
Andrei
Level 41
6 November 2020, 09:09
But why would you scanner when Codegym recommends using BufferedReader?
Adam Odoj
Level 15 , Poland
29 November 2021, 14:35
I agree! I'm still using Buffered Reader but whereever I look for google examples, eveyone uses scanner, that's odd, waiting for answers soon...
Pavel Naumovich
Level 16 , Chippenham, United Kingdom
30 September 2020, 17:15
.split(" ") Character.toUpperCase charAt are your friends