Skip to content

Instantly share code, notes, and snippets.

@Harishhona07
Created April 9, 2023 12:50
Show Gist options
  • Save Harishhona07/0ec1b385a41c778f48d9a6cac94677a4 to your computer and use it in GitHub Desktop.
Save Harishhona07/0ec1b385a41c778f48d9a6cac94677a4 to your computer and use it in GitHub Desktop.
C program to enter names of n numbers of Students and sort them in Alphabetical order.
#include<stdio.h>
#include<string.h>
struct std {
char name[30];
}s[100],temp;
void main() {
int n, i, j;
printf("Enter how many students\n");
scanf("%d",&n);
//Taking Inputs:
printf("Enter %d names:",n);
for(i=0; i<n; i++) {
scanf("%s",&s[i].name);
}
//Sorting in Alphabetical Order using String Handling:
for(i=0; i<n; i++) {
for(j=i+1; j<n; j++) {
if(strcmp(s[i].name,s[j].name)>0) {
strcpy(temp.name,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,temp.name);
}
}
}
//Displaying sorted names:
printf("\n In Alphabetical Order:\n");
for(i=0; i<n; i++) {
printf("%s\n",s[i].name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment