-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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