java - I'm trying to check if a word is abecedarian using compareTo -
i keep program pretty same. i've used lot of string methods charat
, substring
, indexof
. word said “abecedarian” if letters in word appear in alphabetical order.
import java.util.*; public class abecedarian{ public static void main( string[] args ){ string word=getword(); } public static string getword(){ scanner keyboard = new scanner(system.in); string word; system.out.print("enter word: "); word=keyboard.nextline(); return word; } public static boolean isabecedarian(string word){
you iterate on characters in word , make sure in ascending alphabetical order:
public static boolean isabecedarian (string word) { int length = word.size(); if (length <= 1) { return true; } word = word.tolowercase(); // in case char prev; char curr; (int = 1; < length; ++i) { prev = word.charat(i - 1); curr = word.charat(0); if (prev > curr) { return false; } } return true; }
Comments
Post a Comment