A word is said to be \"abecedarian\" if the letters in the word appear in alphab
ID: 3727558 • Letter: A
Question
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 Write a Java program and method called IsAbecedarian that check to see if a word of any size entered from the keyboard is abecedarian or not. Assume the word entered is a legal word. Output should be user friendly.With comments.
Explanation / Answer
import java.util.*;
public class Abecedarian
//the class name named Abecedarian
{
public static boolean isAbecedarian(String str) {
int index_val = 0;
//initialize tha index_val with 0
char ch = 'a';
//let the character ch be initialized as 'a'
int len=str.length();
//the len is the variable which is integer that is used to store the length of the variable
while (index_val < len) {
//if the length of the variable is less than the index-val then the loop continues or otherwise it terminates
if (ch > str.charAt(index_val)) {
return false;
}
//if the character ch is greater than the character of the string in the given index then it returns false
ch = str.charAt(index_val);
index_val = index_val + 1;
//index_val is incremented by one
}
return true;
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String str_name=sc.nextLine();
//getting the string from the user as a input
if(isAbecedarian(str_name)==true)
//if abecedarian condition is true then the given string is an abecedarian
System.out.println("The given string is an abecedarian");
else
System.out.println("The given string is not an abecedarian");
//if abecedarian condition is not true then the given string is not an abecedarian
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.