-2

I'm trying to write a program that reads a word and prints if:

  • it ends with the letter y.

  • has the same first and last character, ignoring case.

This is what I have so far, but I'm having trouble thinking of a line of code that will check individual letters or compare the first and last letters.

  if (. . .)
  {
     System.out.println(word + " ends in a y");
  }

  if (. . .)
  {
     System.out.println(word + " starts and ends with the same letter");
  }      
Rod112
  • 1
  • 2

3 Answers3

1

String has an endsWith method.

if (word.endsWith("y") || word.endsWith("Y")) {
    System.out.println(word + " ends with y");
}

You can get a character from the string using charAt, as long as the string is not empty. You can convert a character to upper case using Character.toUpperCase so that you can compare characters without worrying about what case they are in.

if (word.length() > 0 && Character.toUpperCase(word.charAt(0))==Character.toUpperCase(word.charAt(word.length()-1))) {
    System.out.println(word + " starts and ends with the same letter.");
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

You can use String.endsWith for both:

// To ignore case, just lower all
word = word.toLowerCase();
// Check if it ends with 'y'
if (word.endsWith("y"))
// Check if it starts and ends with same letter
if (word.endsWith(word.substring(0,1)))
Claudio Weiler
  • 589
  • 2
  • 15
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Jul 05 '17 at 21:30
-1

Consider a word hello in your string. You can get the word length easily with word.length() this will return 5 for "Hello"

With another method called charAt(int position) you can get the character at the given position.

System.out.println(String.valueOf(word.charAt(0))); //The result is H System.out.println(String.valueOf(word.charAt(4))); //The result is o

4 is the length of word minus one so try finding it dynamically for all the word this way:

String.valueOf((word.length()-1))

If you had two string you can compare them with:

string1.equals(string2)

this will return true if they are the same and false if they are not.

Below is the full source code:

    String word = "Hello";

    //no if is needed for the first one
    println(word + " ends with letter " + word.charAt(word.length()-1) + ".");


   if (String.valueOf(word.charAt(0)).equals(String.valueOf(word.charAt(word.length()-1)))) {
        println(word + " starts and ends with the same letter.");
    }
Lord Tesla
  • 84
  • 6
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Jul 05 '17 at 21:30
  • I edited my answer. I hope you change your mind and mark my answer as a helpful solution. – Lord Tesla Jul 06 '17 at 05:57
  • Perhaps you should try and compile this code and then fix the problems. – khelwood Jul 06 '17 at 06:19