C program to sort names in alphabetical ascinding order using structures
Sorting names in alphabetical order is a common programming task that can be accomplished using a variety of techniques. One approach is to use structures in C programming language. In this article, we will discuss how to write a C program to sort names in alphabetical ascending order using structures.
Introduction
Sorting names in alphabetical order is a common task in computer programming, and it can be achieved in various ways. However, using structures is one of the most efficient ways to do this. In this article, we will show you how to sort names in alphabetical ascending order using structures in C.
Understanding Structures in C
A structure in C is a collection of related data items that can be of different data types. It allows you to group together variables of different types under a single name, so that they can be manipulated as a single unit. The general syntax for defining a structure in C is as follows:
struct structure_name
{
data_type1 member1;
data_type2 member2;
...
data_typeN memberN;
};
Here, structure_name is the name of the structure, and member1 through memberN are the members or fields of the structure.
Sorting Names Using Structures
To sort names in alphabetical ascending order using structures in C, we can create a structure that holds the names as character arrays. We can then use a sorting algorithm to sort the names in the structure.
There are several sorting algorithms that can be used to sort names in alphabetical order, such as bubble sort, insertion sort, and selection sort. In this article, we will use the bubble sort algorithm.
Writing the C Program
Let's start by writing the C program to sort names in alphabetical ascending order using structures.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
struct student
{
char name[15],add[15];
};
struct student st[50],temp;
int n,i,j;
printf("How many students are there\n");
scanf("%d",&n);
/* Reading data */
for(i=0;i<n;i++)
{
printf("Enter name and address of student\n");
scanf("%s%s",st[i].name,&st[i].add);
}
/* Sorting */
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(st[i].name,st[j].name)>0)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
}
/* Display sorted data */
printf(" \nName\tAddress\n");
for(i=0;i<n;i++)
{
printf(" %s\t%s\n",st[i].name,st[i].add);
}
getch();
}
Explanation of the program
This C program sorts names in alphabetical ascending order using structures. The program first defines a structure called student with two character arrays: name and add, which will store the name and address of the students.
Next, the program prompts the user to enter the number of students they want to sort. The program then reads the names and addresses of each student using a for loop that iterates n times.
After reading the data, the program uses a nested for loop to compare the names of each student and sort them in alphabetical order. If the name of the ith student is greater than the name of the jth student, the program swaps the two students using a temporary variable temp.
Finally, the sorted names and addresses of the students are displayed using another for loop that iterates n times. The program prints the name and address of each student in alphabetical order.
The program uses three header files: stdio.h, conio.h, and string.h. stdio.h is used for standard input/output operations, conio.h is used for console input/output operations, and string.h is used for string operations.
Testing the Program
To test the program, we can compile and run it on our local machine. After running the program, we will be prompted to enter the number of names we want to sort. We can then enter the names one by one, and the program will sort the names in alphabetical order and print them to the console.
Here's an example of how the program works:
Note: To display the string in Descending order just change the line
if(strcmp(st[i].name,st[j].name)>0) into
if(strcmp(st[i].name,st[j].name)<0)
Conclusion
In this article, we discussed how to write a C program to sort names in alphabetical ascending order using structures. We covered the basic concepts of structures in C, and showed how to use the bubble sort algorithm to sort names in alphabetical order. We also provided a complete working example of the program.
FAQs
1) What is a structure in C programming?
Ans: structure in C is a collection of related data items that can be of different data types.
2) What is the bubble sort algorithm?
Ans: The bubble sort algorithm is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
3) Can we sort names in descending order using the bubble sort algorithm?
Ans: Yes, we can sort names in descending order using the bubble sort algorithm by changing the comparison operator in the bubbleSort function to < instead of >.
4) Can we use structures to sort other types of data besides names?
Ans: Yes, structures can be used to sort any type of data that can be compared using a comparison operator.
No comments:
Post a Comment