Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, September 5, 2022

Program to sort input names in alphabetically descending order by using C programming

 Program to sort input names in alphabetically descending order by using C programming





Introduction: 

Sorting is a fundamental operation in computer science and is used in a variety of applications. Sorting names in alphabetical order is a common task in many programs. In this article, we'll explore how Program to sort input names in alphabetically descending order by using C programming. We'll cover the sorting algorithm, show you how to implement it in C, and provide examples to help you understand the process.

The Sorting Algorithm:


To sort input names in alphabetical descending order, we'll use the bubble sort algorithm. This algorithm works by repeatedly swapping adjacent elements if they are in the wrong order. 
Here are the steps of the algorithm:
  1. Compare the first two elements of the list.
  2. If the first element is greater than the second element, swap them.
  3. Move to the next pair of elements and repeat step 2.
  4. Continue until the end of the list is reached.
  5. Repeat steps 1-4 until no more swaps are necessary.

Example Program:

Now that we've seen how to implement the sorting algorithm in C, let's take a look at a complete program that sorts input names in alphabetical descending order.


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,j,n;
printf("enter how many students you want to enter:\n");
scanf("%d",&n);
char name[n][30], temp[30];
printf("enter %d name of student:\n",n);
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])<0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n\nThe sorted name are:");
for(i=0;i<n;i++)
{
printf("\n%s",name[i]);
}
getch();
}

Explanation of the code:


This C code is a program that takes user input of names of students and sorts them in descending order using the selection sort algorithm.

First, the program asks the user to input the number of students they want to enter. Then, it creates a two-dimensional character array name with dimensions n and 30, where n is the number of students entered by the user. It also creates a temporary character array temp of size 30.

Next, the program asks the user to enter the names of the students one by one using a loop and stores them in the name array.

After that, the program uses nested loops to compare each name with all the other names in the array and sort them in descending order using the strcmp() and strcpy() functions from the string library.

Finally, the program prints the sorted names using another loop and the printf() function.

The getch() function is used to hold the console window open until the user presses a key. The main() function is declared as an integer but no value is returned, which is allowed in C, but not recommended.

Output:




FAQ:



Q: What does this program do?
Ans: This program takes user input of names of students and sorts them in descending order using the selection sort algorithm.

Q: What is the purpose of sorting the names in descending order?
Ans: The purpose of sorting the names in descending order is to arrange the names from Z to A, which is the opposite of the default alphabetical order from A to Z.

Q: What is the selection sort algorithm?
Ans: The selection sort algorithm is a simple sorting algorithm that repeatedly selects the smallest element from an unsorted list and swaps it with the first element, then selects the next smallest element and swaps it with the second element, and so on until the entire list is sorted.

Q: What is the function of the strcmp() and strcpy() functions in this program?
Ans: The strcmp() function is used to compare two strings and determine which one comes first in alphabetical order. The strcpy() function is used to copy the contents of one string to another.

Conclusion:

In conclusion, this C program demonstrates the use of the selection sort algorithm to sort a list of names in descending order, which can be useful for various applications such as sorting a list of students by their last names or sorting a list of files by their names in reverse order. The program also utilizes the strcmp() and strcpy() functions from the string library to compare and copy strings, respectively. Overall, this Program to sort input names in alphabetically descending order by using C programming










No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages