Alphabet Soup

Challenge

Using the C# language, 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. 
Sample Test Cases

Input:"coderbyte"

Output:"bcdeeorty"


Input:"hooplah"

Output:"ahhloop"

Alphabet Soup算法把字符串里面的字母按照值(ascii码)从小到大排序,这里用冒泡排序

        public static string AlphabetSoup(string str)
        {
            char[] chArray = str.ToArray();
            int length = chArray.Length;
            char ch = '\0';
            for (int i = 0; i < length; i++)
            {
                for (int j = i + 1; j < length; j++)
                {
                    if (chArray[i] > chArray[j])
                    {
                        ch = chArray[i];
                        chArray[i] = chArray[j];
                        chArray[j] = ch;
                    }
                }
            }
            return new string(chArray);
        }



你可能感兴趣的:(Coderbyte算法题目)