Have the function AlphabetSoup(str) take the str string parameter being passed and return the string with the letters in alphabetical order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be included in the string.
Alphabet Soup Java
import java.util.Arrays;
import java.util.Scanner;
public class AlphabetSoup {
String AlphabetSoup(String str) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
AlphabetSoup c = new AlphabetSoup();
System.out.print(c.AlphabetSoup(s.nextLine()));
}
}
Alphabet Soup Javascript
function alphabetSoup1(str) {
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var newStr = "";
for (i = 0; i < alphabet.length; i++) {
for (j = 0; j < str.length; j++) {
if (alphabet[i] === str[j].toLowerCase()) {
// add to alphebetical string
newStr += str[j];
// pop out that letter from str
str = str.slice(0, i) + str.slice(i);
}
}
// dont go through entire alphabet
if (str.length === 0) {
break;
}
}
return newStr;
}
function alphabetSoup2(str) {
var array = str.split('');
return array.sort().join('');
}
Alphabet Soup Python
def AlphabetSoup(strng):
ary = list(strng)
ary.sort()
return ''.join(ary)
def main():
result = AlphabetSoup("hooplah")
print result
if __name__ == "__main__":
main()