Here I’m going to write program logic to sort string in alphabetical order in C#.Net.
It is one of the most important interview questions for fresher as well as experienced candidates. It is a very important program where you will get the result in just 3-4 lines of code using two for loops.
Check this
video for your reference.
- using System;
- namespace ConsoleAppInterviewLogical
- {
- class SortStringAlpha
- {
- static void Main(string[] args)
- {
- char temp;
- string myStr = "Gautam";
- 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];
- charstr[j + 1] = temp;
- }
- }
- }
- Console.WriteLine(charstr);
- Console.ReadLine();
- }
- }
- }
Hope you like the post, if you found this post useful, please like the post and provide your feedback. that motivates me to create more useful stuff.