2

In the code below, I tried to compare char at i with the char at i+1. My understanding is that by using charAt(): I can take the character from the string and treat it as integer and be able to compare two characters. This part of the code works, but I think I am missing something in the code, hence it is not printing the desired result. Unless this way of sorting characters in a String is not valid.

public class stringAlphabetical {

    public static void main(String[] args){
        String word="watch";
        boolean swapped;
        char temp = ' ';
        do{
            swapped = false;
            for(int i=0;i<word.length()-1;i++){
                char a = word.charAt(i);
                char b = word.charAt(i+1);

                if(word.charAt(i)>word.charAt(i+1)){   // if (a>b) {
                   temp = a;
                   a = b;
                   b = temp;
                }
            }


        }while (swapped==true);

        System.out.println(word);
    }
}
Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34
johnny87
  • 21
  • 1
  • 3
  • What is your purpose for avoiding arrays? Using a mutable data structure like an array or a `List` is the intended way to do operations like sorting. – dimo414 Jul 01 '17 at 04:52
  • Also, you never update the value of `word`, so this code just prints the original input again. – dimo414 Jul 01 '17 at 04:53

3 Answers3

2

Java String is immutable, so you will need to use a mutable class (like StringBuilder) - (also, you are modifying char values, not a reference) and you don't need t.

StringBuilder word = new StringBuilder("watch");
boolean swapped;
do {
    swapped = false;
    for (int i = 0; i < word.length() - 1; i++) {
        char a = word.charAt(i), b = word.charAt(i + 1);

        if (a > b) { // <-- this is fine.
            word.setCharAt(i, b);
            word.setCharAt(i + 1, a);
            swapped = true;
        }
    }
} while (swapped);
System.out.println(word);

Which outputs

atchw

Or just use an array (for the same result)

String word = "watch";
char[] c = word.toCharArray();
Arrays.sort(c);
System.out.println(new String(c));
shmosel
  • 49,289
  • 6
  • 73
  • 138
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Use this code for sort the String array via alphabetical order without storing in any array

    Scanner kbd = new Scanner(System.in);
    String input = kbd.nextLine();
    String sortedString = Stream.of(input.split("")).sorted().collect(Collectors.joining());
    System.out.print(sortedString);
0

To sort the string alphabetically, you need to compare each character with all the characters and if the condition satisfies then you swap the character. This is shown using multiple loops. Lastly I print the char array.

public static void main(String[] args){
        String watchString = "watch";
        int j;
        char temp;

        char[] chars = watchString.toCharArray();

        for (int i = 0; i <chars.length; i++) {

            for ( j = 0; j < chars.length; j++) {

                if(chars[j]>chars[i]){
                    temp=chars[i];
                    chars[i]=chars[j];
                    chars[j]=temp;
                }

            }

        }

        for(int k=0;k<chars.length;k++){
            System.out.print(chars[k]);
        }

    }
lambad
  • 1,046
  • 10
  • 21