Write a program that uses the String
method compareTo
to compare two words that are input by the user. Output if the first word is less than, equal to or greater than the second word.
import java.util.Scanner;
public class Comparing_String {
public static void main(String[] args) {
System.out.println("Comparing Strings");
Scanner scanner = new Scanner(System.in);
System.out.println("First Word?");
String word1 = scanner.nextLine();
System.out.println("Second Word?");
String word2 = scanner.nextLine();
if(word1.compareTo(word2) < 0) {
System.out.println(word1 + " is less than " + word2);
}
else if (word1.compareTo(word2) == 0) {
System.out.println(word1 + " is equal to " + word2);
}
else {
System.out.println(word1 + " is greater than " + word2);
}
}
}
First Word?
> Hello
Second Word?
> hello
Hello is less than hello
First Word?
> among
Second Word?
> amend
among is greater than amend
First Word?
> apple
Second Word?
> orange
apple is less than orange
First Word?
> Hello
Second Word?
> Hello
Hello is equal to Hello
The input to your program will be a sentence. Your program has to determine if the input sentence is a palindrome. You should consider upper and lowercase versions of the same letter to be equal and ignore non-alphabetical characters.
import java.util.Scanner;
public class Sentence_Extract_Words {
public static boolean is_palindrome (String input) {
String y = "";
for (int i = input.length() -1 ; i >= 0; i--) {
y += input.charAt(i);
}
// y.equals(String) would check equality, but would consider
// upper- and lowercase letters different
if (y.equalsIgnoreCase(input)) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
System.out.println("Checking for Palindromes");
Scanner scanner = new Scanner(System.in);
System.out.println("Input Your Sentence");
String sentence = scanner.nextLine();
String letters = "";
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) >= 'a' && sentence.charAt(i) <= 'z') {
letters += sentence.charAt(i);
}
}
if (is_palindrome(letters) == true) {
System.out.println(sentence + " is a palindrome ");
}
else {
System.out.println(sentence + " is not a palindrome ");
}
}
}
> Madam I'm Adam.
The input sentence is a palindrome
> Able was I ere I saw Elba.
The input sentence is a palindrome
> I played soccer all day.
The input sentence is not a palindrome
The input to your program will be a word. Your program should do the following:
import java.util.Scanner;
public class RemoveDuplicateLetters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println ("Input Word");
String word = scanner.nextLine();
String ans = "";
for (int i = 0; i <word.length(); i++) {
int count = 0;
for (int j = 0; j <ans.length(); j++) {
if (word.charAt(i) == ans.charAt(j)) {
count++;
}
}
if (count == 0) {
ans += word.charAt(i);
}
}
if (word.equals (ans)) {
System.out.println("No repeated letters");
} else {
System.out.println (ans);
}
}
}
> mattress
matres
> fine
No repeated letters