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

Time:03-15

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;    
}

CodePudding user response:

I'm not good at English. Do you mean that you want to check whether a string equals to abecedarian?

You may try str.toLowerCase().equals("abecedarian")

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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;
}
  •  Tags:  
  • java
  • Related