walle
walle

Reputation: 19

Is there a more effective way to write a function that checks if a word is abecedarian? JAVA

Here is a code that I wrote for my assignment:

Write a method called isAbecedarian that takes a String and returns a boolean indicating whether the word is abecedarian (characters arranged alphabetically), ignoring case.

I am trying to figure out if there is a better way to write this function than what I wrote here:

public static boolean isAbecdarian(String s) { 
    int index = 0;
    char c = 'a'; 
    while (index < s.length()) {
        if (c > s.charAt(index)) {
            return false;
        }
        c = s.charAt(index); 
        index = index + 1;
            
    }
    return true;    
}

Upvotes: 0

Views: 257

Answers (3)

Stephen C
Stephen C

Reputation: 719189

Here is a better way:

public static boolean isAbecdarian(String s) { 
    String upper = s.toUpperCase();
    for (int i = 1; i < upper.length; i++) {
        if (upper.charAt(i - 1) > upper.charAt(i)) {
            return false;
        }
    }
    return true;
}

Admittedly, the differences are relatively small. But (IMO) the code is easier to understand with a for loop and no temporary variables.

And you forgot the "ignore case" requirement.

Finally, I have intentionally not added an explicit null check to avoid NPEs. That would (IMO) be incorrect. The spec you have been given doesn't require this, and this is not a reasonable requirement to infer in this context. It is better to let an unexpected (and incorrect per the spec) null argument produce an NPE for the programmer to notice and fix.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425178

How about a 1 liner:

public static boolean isAbecdarian(String s) {
    return s.toLowerCase().chars().reduce(0, (a, b) -> a >= 0 && b >= a ? b : -1) >= 0;
}

Upvotes: 0

Abra
Abra

Reputation: 20914

I believe the task is to determine whether the letters of a particular word are arranged alphabetically, for example the word act since the letter c comes after the letter a in the English alphabet and similarly the letter t comes after the letter c.

Notes after the code.

public class Abecedarian {
    private static boolean isAbecedarian(String word) {
        boolean isAbecedarian = false;
        if (word != null) {
            int length = word.length();
            if (length == 1) {
                isAbecedarian = true;
            }
            else if (length > 1) {
                word = word.toLowerCase();
                char c = word.charAt(0);
                for (int i = 1; i < length; i++) {
                    if (c <= word.charAt(i)) {
                        isAbecedarian = true;
                        c = word.charAt(i);
                    }
                    else {
                        isAbecedarian = false;
                        break;
                    }
                }
            }
        }
        return isAbecedarian;
    }

    public static void main(String[] args) {
        System.out.println(isAbecedarian("I"));
        System.out.println(isAbecedarian("art"));
        System.out.println(isAbecedarian("cat"));
    }
}
  • A single letter word must be abecedarian.
  • For words with more than one letter, each successive letter must have a unicode code point that is greater than, or equal to, its predecessor letter. For letters in the English alphabet the unicode code point is the same as the ASCII code.

The above code prints the following output:

true
true
false

Upvotes: 1

Related Questions