Reputation:
I am using Java to create a method called isOrdered to tell if a string is sorted naturally in alphabetical order. For example, if the input was "effort" or "Aaaabbyy" the method would return true, compared to "hello" that would return false since the letter 'h' appears after the letter 'e'.
I have worked out this so far,
public class orderWording {
public static void main(String[] args) {
System.out.println(isOrdered("effort")); //should appear true
}
public static boolean isOrdered (String s) {
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) == /*alphabet */ ) {
return true;
}
else {
return false;
}
}
}
}
However, I don't know how to match each character with the alphabet to know if the string is naturally in alphabetical order.
I thought about making a nested loop within the isOrdered method. While the first loop is traversing the string, the second loop is matching those characters with the order of the alphabet.
Something similar to this,
public static boolean isOrdered (String s) {
String a = 'abcdefghijklmnopqrstuvwxyz'
for (int i = 0; i < s.length(); i ++) {
for (int j = 0; j < a.length(); j ++){
if (s.charAt(i) == a.charAt(j) ) {
return true;
}
else {
return false;
}
}
}
}
Although, I am not sure if this is a correct way to solve this problem.
Thank you for your help.
Upvotes: 0
Views: 641
Reputation: 199
I think this should solve your issue. I added another method to check if it sorted in descending fashion as well.
public class OrderWording {
public static void main(String[] args) {
System.out.println(isAscendingOrdered("effort")); //true
System.out.println(isAscendingOrdered("java")); //false
System.out.println(isDescendingOrdered("Yea")); //true
}
public static boolean isAscendingOrdered (String s) {
s=s.toUpperCase();
for (int i = 0; i < s.length()-1; i ++) {
if (s.charAt(i)>s.charAt(i+1))
return false;
}
return true;
}
public static boolean isDescendingOrdered (String s) {
s=s.toUpperCase();
for (int i = 0; i < s.length()-1; i ++) {
if (s.charAt(i)<s.charAt(i+1))
return false;
}
return true;
}
}
Hope this helps. Cheers!
Upvotes: 1
Reputation: 638
Try this
public class orderWording {
public static void main(String[] args) {
System.out.println(isOrdered("effort")); //should appear true
}
public static boolean isOrdered (String s) {
boolean issorted=true;
for (int i = 0; i < s.length()-1; i ++) {
if (Character.toLowerCase(s.charAt(i)) >Character.toLowerCase(s.charAt(i+1) ) {
issorted=false;
break;
}
} return issorted;
}
}
Upvotes: 0
Reputation: 17454
You can make use of sort method from Java. Create a simple method to sort String:
public static String sortString(String str)
{
// convert string to char array
char chrArray[] = str.toCharArray();
// sort string in array
Arrays.sort(chrArray);
// return sorted string
return new String(chrArray);
}
To check if your String is sorted:
String str1 = "effort";
str1.equlsIgnoreCase(sortString(str1)); //true
String str2 = "apples";
str2.equlsIgnoreCase(sortString(str2)); //false
Upvotes: 0