8.14 Exercises Exercise 8.1 A word is said to be \"abecedarian\" if the letters
ID: 3891439 • Letter: 8
Question
8.14 Exercises Exercise 8.1 A word is said to be "abecedarian" if the letters in the word appear in alphabetical order. For example, the following are all 6-letter English abecedarian words. abdest, acknow, acorsy, adempt, adipsy, agnosy, befist, behint, beknow, bijoux, biopsy, cestuy, chintz, deflux, dehors, dehort, deinos, diluvy, dimpsy a. Describe an algorithm for checking whether a given word (String) is abecedarian, assuming that the word contains only lower-case letters. Your algorithm can be iterative or recursive. b. Implement your algorithm in a function called IsAbecedarian).Explanation / Answer
Solution:
import java.util.Scanner;
public class Abecedarian {
// Driver method to check the functionality of the isAbecedarian
public static void main( String[] args ){
Scanner scan = new Scanner(System.in);
//Taking the input from the user
System.out.println("Enter a word to check for abecedarian");
String word=scan.nextLine().toLowerCase();
//If isAbecedarian returns true that means the given word is an abecedarian else its not.
if(isAbecedarian(word))
System.out.println("The given word is Abecedarian.");
else
System.out.println("The given word is not an Abecedarian.");
}
//Abecedarian method will return true if the given word is abecedarian else it will return false.
public static boolean isAbecedarian(String s) {
int index = 0;
char c = 'a';
//Iterating over the word and checking each character with the previous letter
while (index < s.length()) {
if (c > s.charAt(index)) {
return false;
}
c = s.charAt(index);
index = index + 1;
}
return true;
}
}
Sample Run 1:
Enter a word to check for abecedarian
education
The given word is not an Abecedarian.
Sample Run 2:
Enter a word to check for abecedarian
biopsy
The given word is Abecedarian.
Note: If you feel any issue/doubt please do comment below before giving any negative feedback. If you liked the solution the don't forget to give a big thumbs up
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.