trama
trama

Reputation: 1401

Best way to check if String is in alphabetical order?

This is the most intuitive way to check if a five character String is in alphabetical order?

String newStr = "Hello";

if (newStr.charAt(0) <= newStr.charAt(1) &&
    newStr.charAt(1) <= newStr.charAt(2) &&
    newStr.charAt(2) <= newStr.charAt(3) &&
    newStr.charAt(3) <= newStr.charAt(4)) {
        System.out.println("In order");
} else
    System.out.println("NOT in order");

Upvotes: 0

Views: 13280

Answers (7)

DnR
DnR

Reputation: 3507

If you are looking for the most simplest one, then perhaps this is what you are looking for:

public static void main(String[] args) {
    String str = "Hello";
    System.out.println(alphabeticalOrder(str.toLowerCase()));

}
public static boolean alphabeticalOrder(String str){
    for(int i = 0;i<str.length()-1;i++)         
        if(str.charAt(i) > str.charAt(i+1))
            return false; 
    return true;
}

Upvotes: 1

Shirish Bari
Shirish Bari

Reputation: 2722

It seems that the logic needs to be improved

for input String "AbCd" & HeLLo code is printing not in order

You have tried to compare the ascii values of characters and you know that for 
same case letters (lower Case or Upper Case) ascii values would be in increasing order. 
Hence your logic stays intact if input string is in same case : 
e.g. "hello" ,"HELLO", "abcd" or "ABCD" 

If you are using string with Mixed Case letters then it wont work correctly.

Upvotes: 0

Yagnesh Agola
Yagnesh Agola

Reputation: 4639

You may use Regular Expression like below :

public boolean checkString(String str)  
{
   String str = "^a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$";
   Pattern pattern = null;
   Matcher matcher;
   try{
         pattern = Pattern.compile(str,Pattern.CASE_INSENSITIVE);

         System.out.println(matcher.matches());*/
         matcher = pattern.matcher("Hello");
         return matcher.matches();
      }
   catch(Exception ex){
         ex.printStackTrace();
        return false;
   }  
}   

Your Main class or from where you call above function :

String myStr = "Hello";  
boolean isAlphaOrder = checkString(myStr);  
if(isAlphaOrder)  
  System.out.println("String is in order");
else
   System.out.println("NOT in order");

Upvotes: 1

Raju Sharma
Raju Sharma

Reputation: 2516

you may want this:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        String str="Hello";
        str=str.toLowerCase();
        char[] ch=str.toCharArray();
        Arrays.sort(ch);
        String str1="";
        for(int i=0;i<ch.length;i++)
        {
            str1+=ch[i]+"";
        }
        if(str1.equals(str))
        {
            System.out.println("In order");
        }
        else
        {
            System.out.println("NOT in order");
        }



    }

Upvotes: 0

BaptisteL
BaptisteL

Reputation: 499

You have to be aware that letter in capital have not the same index that their letter not in capital.

So if you want that abcDe return true, you should also use a tempory String to do you check like this :

String yourString ="..."
String tmp = yourString.toLowerCase();
boolean isInOrder = testYourString(tmp);

Upvotes: 0

maximus_de
maximus_de

Reputation: 382

In your case this might be good enough. But think of it as you want to do this to Strings longer than five characters... than it will be a very long if-statement and much more work to do.

I'd rather suggest to use a loop, then you dont need to fix the length of your String.

I.e.

boolean sorted = true;
for(int i = 0; i < newStr.length()-1; i++){
    if(newStr.charAt(i) >= newStr.charAt(i+1)){
      sorted = false;
      break;
    }
}
if(sorted){
    System.out.println("In order");
}else{
    System.out.println("NOT in order");
}

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You can try this

public static void main(String[] args) {
    String str = "Hello";
    char[] newStr = str.toCharArray();
    char previous = '\u0000';
    isInOrder(previous,newStr); 

}

private static boolean isInOrder(char previous, char[] arr) {
    for (char current : arr) {
        if (current < previous)
            return false;
        previous = current;
    }
    return true;
}

Upvotes: 3

Related Questions