This program compares two words to see if the first one is inside the second one and then prints the result.
My Code
import java.util.Scanner;
public class WordInsideWord {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type the first word: ");
String firstWord = reader.nextLine();
System.out.print("Type the second word: ");
String secondWord = reader.nextLine();
int index = firstWord.indexOf(secondWord);
if (index == -1) {
System.out.println("The word " + secondWord + " is not found in the word " + firstWord + ".");
} else {
System.out.println("The word " + secondWord + " is found in the word " + firstWord + ".");
}
}
}
Model Code
import java.util.Scanner;
public class WordInsideWord {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type the 1. word:");
String word1 = reader.nextLine();
System.out.print("Type the 2. word:");
String word2 = reader.nextLine();
if (word1.indexOf(word2) != -1) {
System.out.println("The word '" + word2 + "' is found in the word '" + word1 + "'");
} else {
System.out.println("The word '" + word2 + "' is not found in the word '" + word1 + "'");
}
}
}
Comments
Mine is pretty much the same as the Model Code except that this time I have introduced a new variable to make it easier to read and they have not.