You need to make your own notes about Strings and String methods.
You absolutely must know how to use the following string methods: length(), charAt(), substring(), indexOf(),
as well as changing a String to a character array and vice versa.
There are some other very simple methods that you also need to know: toUpperCase(), toLowerCase(), trim()
.
Strings are very useful and important, so the more you know about handling and manipulating them the better.
Some of these are quite hard to do, but take time and work out the procedure that you think should work - probably before you start coding.
To learn more please see Strings at coding bat.com
For all of these programs, you should write the program so that it works for all cases, including edge cases.
For example, when you try to print out the second word, what will happen if there is only one word?
What happens if the String is zero length? (You don't have to consider String = null)
The green programs should not be too difficult.
ThirdWord.java use split()
Write a program that reads in a sentence (use Scanner) and then prints out every third word
VowelCount.java use charAt()
Count how many vowels there are in a word (or any string).
Do this using a method that takes the string and returns the number of vowels. Make this method use "charAt()".
Now write a second method that uses "toCharArray()" and do the same thing. (Both methods should produce the same answers)
CountingThe.java use indexOf()
Count how many times the word "the" or "The" appears in a sentence. Print out the number.
This would also include "then", "either", etc.
(After doing this with indexOf() you could probably also do it with charAt() )
SecondWord.java Do not use .split()
Use indexOf() and substring() to copy the second word of a sentence into a variable and then print it out
KingFredOfId.java
See instructions here.
Cutting and splicing
Write a program that gets a sentence.
If the sentence contains the word "secret" or "secrets", use indexOf() and substring()
to remove that word and put the rest of the sentence back together into a string.
Print out the string.
It looks like this is just for the first occurrence of the word "secret".
You could easily expand it to do all occurrences, and also change it to a method that will remove any desired word.
So you would run removeWord("secrets") and then removeWord("secret").
Do the two green and two red programs. If you can do the two blue ones as well, that's amazing!
Triplets.java
Read in a sentence and print out only words that have the same letter repeated 3 or more times in a row (e.g. happpy)
StringBld.java You cannot call this "StringBuilder" because that's already a class in Java.
Write a program that uses the StringBuilder class. Use 3 methods that are not available in the String class.
For example: append(), capacity(), delete(), insert(), replace()
Convert a string to a stringBuilder and then vice versa (for example when you want to print it out).
PostalCode.java ** Omit this program (Nov 2021)
Write a program to generate random Canadian postal codes. The first letter should be a random letter from M-R (inclusive). The program should print out 5 postal codes.
Ignore the fact that postal codes do not include D, F, I, O, Q, U, W, Z
Numbers.java
Take a string and for each digit in the string, increment it by one. 9 becomes 0 instead of 10.
Example: "That's his 5th sneeze" → "That's his 6th sneeze" | "abc351def90" → "abc462def01"
RandomWord.java (you'll probably find the int->char conversions useful)
Make random 6 letter words with exactly one or two vowels in them (randomly decide if the string will have 1 or 2 vowels).
The vowels should be located at random locations. (eg. "yupsqt", "ihlkpa")
Anagram.java NO, OMIT THIS
Given a 4 letter word, print out all of the combinations of letters to make another word.
e.g. live: eilv eivl eliv elvi evil evli ielv ievl ilev ilve ivel ivle leiv levi liev live lvei lvie veil viel veli vile vlei vlie (24 words)
(There will be n! results, where n = number of letters)
DO NOT COPY the complex and strange algorithm that you can find by just googling this problem online.
Write your own solution if you do this question.
Never mind. It's too easy to copy answers from the internet for this program.
It still is a useful thing to figure out on your own if you want to.
Do at least one of the following two programs. Do both if you're in the top 10%.
LettersInCommon.java
Given a list of 6 letter words {http://listofrandomwords.com/}, and one other word,
find the largest number of letters that this word has in common with any word in the list.
e.g. String[] wordlist = {guyana, lotion, chilly, doggie, fledgy, swanky, crawly, physic};
String word = "digger";
The word "digger" has 5 letters in common with one of the words in the list (the word would be doggie).
The program should print out "5 doggie",
i.e. the max number of letters in common and the word that it matches with to get this number.
Border of stars
Make a program that reads in a sentence using Scanner class.
Now split the sentence at the last space before you get to 20 letters
Set "20" to be a constant (ie. final static variable), so we can change it to 25 or something and it will still work
Printout the sentence surrounded by ****. The *** must form a box and have at least 1 space to the left and right of the text.
If there is no space in the first 20 letters, print an error message exit the program (or else it will probably crash)
The output should look like this.
Please enter a line of text:
"The unusually warm weather makes me think of summer. It is hard to stay focused on mundane work." <ENTER>
************************
* The unusually warm *
* weather makes me *
* think of summer. It *
* is hard to stay *
* focused on mundane *
* work. *
************************