Sort a string alphabetically
August 19, 2022 2022-08-19 19:59Sort a string alphabetically
public static void SortAlphabatically()
{
char temp;
string myStr = "Gautam"; // Here you can take input from user and assign to myStr variable
string str = myStr.ToLower();
char[] charstr = str.ToCharArray();
for (int i = 1; i < charstr.Length; i++)
{
for (int j = 0; j < charstr.Length - 1; j++)
{
if (charstr[j] > charstr[j + 1])
{
temp = charstr[j];
charstr[j] = charstr[j + 1];
var a = charstr[j];
charstr[j + 1] = temp;
var b = charstr[j + 1];
}
}
}
Console.WriteLine(charstr); //aagmtu
Console.ReadLine();
}

Please comment and share if you like this post and tell us about how we can enhance our posts. Thanks.
Subhajit
This is Subhajit Sinha, with more than 8 years of experience as a full stack developer in renowned Multinational Companies, here in this portal as an online tutor I will guide you personally to level up your programming skills and also will coach you to augment your career and salary as well.
Search
Popular posts
