简体   繁体   中英

How to compare last two letters of a string in alphabetical order in Java?

I am having a problem comparing last two alphabetic letters of a single string in alphabetical order. I know how to compare two strings alphabetically using compareTo() string function but no idea about this.

This is what I have to do :

Method isIncreasing() to check whether the given string is an increasing string or not. It is increasing if, while moving from Left to Right, each character in the string comes after the previous character in the alphabetical order.

Example: ANT is an increasing string whereas APPLE is not.

I won't do your homework for you, but here's an outline to get you started:

  1. Create a char variable called for instance last to hold the previous letter.
  2. Initialize it with '\\0'
  3. Iterate over the characters in the string. (Hint: String.toCharArray )
  4. If current character is less than last
    1. Quit with the message "not increasing" otherwise continue to the next character
  5. After the loop, quit with the message "increasing" .

您可以编写一个Comparator来覆盖compare方法,以比较for。循环中每个受Math.min(o1.length,o2.length)限制的字母的短值字母(只是强制转换为short)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM