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".
Going national
- 10
Locked
Comments (35)
- Popular
- New
- Old
You must be signed in to leave a comment
Matar29Expert
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.
0
Shamil
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.
0
RodYokoo
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.
0
HelloWorld
22 January 2022, 08:29
Why can't we do this instead of turning the string to a char array?
+1
Gellert Varga
20 May 2022, 20:00
👍
0
Shamil
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".
0
Anonymous #10775689
1 August 2021, 11:27
Java Program to Capitalize the first character of each word in a String
This works and is an interesting method.
0
Anonymous #11074513
22 September 2022, 00:49
這真的是太棒了
0
Justin Smith
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.
+2
Adam Odoj
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!!!
0
Naughtless
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.
+4
ImDevin
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!!
0
ayhem bouabid
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.
0
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.
+2
ImDevin
3 May 2021, 02:48
and .trim()
+1