The title might not be descriptive but stay with me. This is how buble sorting should look like:
string s = Console.ReadLine();
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.Length; i++)
{
for (int j = 0; j < sb.Length - 1; j++)
{
if (sb[j] > sb[j + 1])
{
char tmp = sb[j];
sb[j] = sb[j + 1];
sb[j + 1] = tmp;
}
}
}
s = sb.ToString();
Console.WriteLine(s);
However I accidentally wrote it like this and I don't know why is this working shouldn't I at least get a indexOutOfRange exception? ->
string s = Console.ReadLine();
StringBuilder str = new StringBuilder(s);
for (int i = 0; i < s.Length; i++)
{
for (int j = 0; j < str.Length; j++)
{
if (str[i] < str[j])
{
var tmp = str[j];
str[j] = str[i];
str[i] = tmp;
}
}
}
s = str.ToString();
Console.WriteLine(s);
Just to clarify this orders the letters of a word in alphabetical order.