版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79747392
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);
}