Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Check if each letter of a given word is less than the one before it

Java Basic: Exercise-248 with Solution

From Wikipedia, An abecedarium (or abecedary) is an inscription consisting of the letters of an alphabet, almost always listed in order. Typically, abecedaria (or abecedaries) are practice exercises.
Write a Java program to check if each letter of a given word (Abecadrian word) is less than the one before it.

Sample Solution:

Java Code:

import java.util.*;
public class solution {	
 public static boolean is_abecedarian_word(String word) {
        int index = word.length() - 1;

        for (int i = 0; i < index; i++) {

            if (word.charAt(i) <= word.charAt(i + 1)) {
            }

            else {
                return false;
            }
        }
        return true;
    }
     

   public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Input a word: ");
		String word1 = scanner.nextLine();
		System.out.println("Is Abecadrian word? "+is_abecedarian_word(word1));		
		}
 }

Sample Output:

Input a word:  ABCD
Is Abecadrian word? true

Flowchart:

Flowchart: Check if each letter of a given word is less than the one before it.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program which accepts three integers and check whether sum of the first two given integers is greater than third one. Three integers are in the interval [-231, 231 ]
Next: Write a Java program to count the number of set bits in a 32-bit integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe