-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasswork7ArraySortingCharacters.java
More file actions
52 lines (45 loc) · 1.06 KB
/
Classwork7ArraySortingCharacters.java
File metadata and controls
52 lines (45 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
//Jonathan Rumley
//CSC 160.401
//March 11, 2020
//Chapter 7 Classwork
public class Classwork7ArraySortingCharacters
{
public static void main(String[] args)
{
//trims out the spaces
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string: ");
String s = input.nextLine();
s = sort(s);
System.out.println(s.trim());
}
public static String sort(String str)
{
//converting all str to LOWERCASE
str = str.toLowerCase();
//Creates array named ch with data type of char
char[] ch = str.toCharArray();
for(int x=0; x< ch.length - 1; x++)
{
char currentMin = ch[x];
int currentMinIndex = x;
for(int y = x + 1; y < ch.length; y++)
{
if(currentMin > ch[y])
{
currentMin = ch[y];
currentMinIndex = y;
}
}
if(currentMinIndex != x)
{
ch[currentMinIndex] = ch[x];
ch[x] = currentMin;
}
}
//converts array ch to String type as variable s
String s = new String(ch);
return s;
}
}