This is an exercise that asks to:
Write a program that first asks a user to enter two words and a letter. Then print the first word in uppercase letters and the second word in lowercase letters. Print out the number of letters in both words, how many times the inputted letter occurs in both words, and which positions in each word the letter occurs (assuming zero-indexed Strings).
After this, print whether the words are the same or not, and finally print out both words backwards. You can use either a
while
loop orfor
loop if needed for this exercise.And the sample output would be something like:
Key point to remember:
- Case sensitivity is irrelevant e.g. (if SHooT and sHOot are respectively entered, it would still return that they are the same words and both have 2 letter o's, which are also occurring in the same positions.
I was successful in coding the program:
import java.util.Scanner; //Imports the Scanner
public class exercise2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //Creates a new scanner called 'scan'
String word1, word2, comparison1, comparison2;
char letter, letter1, letter2;
int counter1, counter2, lettercounter1, lettercounter2;
lettercounter1 = lettercounter2 = 0;
System.out.print("Enter two words: ");
word1 = scan.nextLine();
word2 = scan.nextLine();
comparison1 = word1.toUpperCase();
comparison2 = word2.toUpperCase();
System.out.print("Enter a letter: ");
letter = scan.next().charAt(0);
letter1 = Character.toUpperCase(letter);
letter2 = Character.toLowerCase(letter);
System.out.print(word1.toUpperCase());
System.out.print(" " + word2.toLowerCase());
counter1 = word1.length();
counter2 = word2.length();
System.out.println("\nThere are " + counter1 + " letters in " + word1);
System.out.println("There are " + counter2 + " letters in " + word2);
for (int x = 0; x < word1.length(); x++) {
if (word1.charAt(x) == letter1) {
lettercounter1++;
}
else if (word1.charAt(x) == letter2) {
lettercounter1++;
}
}
for (int x = 0; x < word2.length(); x++) {
if (word2.charAt(x) == letter1) {
lettercounter2++;
}
else if (word2.charAt(x) == letter2) {
lettercounter2++;
}
}
System.out.println("There are " + lettercounter1 + " " + letter + "'s in " + word1);
System.out.println("There are " + lettercounter2 + " " + letter + "'s in " + word2);
if (lettercounter1 == 1) {
System.out.print(letter + " occurs in position ");
for (int x = 0; x < word1.length(); x++) {
if (word1.charAt(x) == letter1) {
System.out.print(x + 1);
}
else if (word1.charAt(x) == letter2) {
System.out.print(x + 1);
}
}
System.out.println(" in " + word1);
}
else if (lettercounter1 > 1) {
System.out.print(letter + " occurs in positions ");
for (int x = 0; x < word1.length(); x++) {
if (word1.charAt(x) == letter1) {
System.out.print(x + 1 + ",");
}
else if (word1.charAt(x) == letter2) {
System.out.print(x + 1 + ",");
}
}
System.out.println(" in " + word1);
}
if (lettercounter2 == 1) {
System.out.print(letter + " occurs in position ");
for (int x = 0; x < word2.length(); x++) {
if (word2.charAt(x) == letter1) {
System.out.print(x + 1);
}
else if (word2.charAt(x) == letter2) {
System.out.print(x + 1);
}
}
System.out.println(" in " + word2);
}
else if (lettercounter2 > 1) {
System.out.print(letter + " occurs in positions ");
for (int x = 0; x < word2.length(); x++) {
if (word2.charAt(x) == letter1) {
System.out.print(x + 1 + ",");
}
else if (word2.charAt(x) == letter2) {
System.out.print(x + 1 + ",");
}
}
System.out.println(" in " + word2);
}
if (comparison1.equals(comparison2)) {
System.out.println(word1 + " and " + word2 + " are the same words.");
}
else {
System.out.println(word1 + " and " + word2 + " are not the same words.");
}
int y, z;
y = counter1 - 1;
z = counter2 - 1;
while (y >= 0) {
System.out.print(word1.charAt(y));
y--;
}
System.out.print(" ");
while (z >= 0) {
System.out.print(word2.charAt(z));
z--;
}
}
}
The program works flawlessly and my only questions are:
- Would there be any way to further simplify this code? (e.g. making it as short as possible, using other simpler methods, etc.)
- Any indentation mistakes? Any suggestions?