Answer :
Answer:
Following are the program in the C# Programming language.
//header
using System;
//define class
public class SortWords
{//define function
public static void Display_Words(string[] word)
{//set string type variable
string a;
//set the for loop for swapping the words
for (int i = 0; i < word.Length; i++)
{//set the for loop
for (int j = 0; j < word.Length - 1; j++)
{
//set the if conditional statement for compare the string
if (String.Compare(word[j], word[j + 1], StringComparison.Ordinal) > 0)
{
//perform swapping
a = word[j];
word[j] = word[j + 1];
word[j + 1] = a;
}
}
}
//print message
Console.WriteLine("Sorted Words: ");
//set for loop to print the sorted list
for (int i = 0; i < word.Length; i++)
{//print space between the sorted characters
Console.Write(word[i] + " ");
}
}
//define the main function
public static void Main()
{//print message
Console.Write("Enter the number of words: ");
//get input from the user
int get_size = Convert.ToInt32(Console.ReadLine());
//set string type array
string[] word = new string[get_size];
Console.WriteLine("Enter " + get_size + " words");
//set for loop to get input in string type array
for (int i = 0; i < get_size; i++)
{
word[i] = Console.ReadLine();
}
//call the function
Display_Words(word);
}
}
Output:
Enter the number of words: 5
Enter 5 words
h
e
l
l
o
Sorted Words:
e h l l o
Explanation:
Here, we define a class named "SortWords" and inside the class.
- Define void type function "Display_words" and pass an string data type array argument "word".
- Inside the function, we set two for loops to swapping the words and inside it we set the if conditional statement to compare the words then, perform swapping.
- Set the for loop to print the sorted list
Finally, we define the main function in which we get inputs from the user, firstly we get the number of words then we get the words from the user. then call the function "Display_words".