Skip to content

Instantly share code, notes, and snippets.

@coderaven
Created June 28, 2013 16:05
Show Gist options
  • Save coderaven/5885850 to your computer and use it in GitHub Desktop.
Save coderaven/5885850 to your computer and use it in GitHub Desktop.
Alphabetical Names Sorter in C
#include <stdio.h>
#include <string.h>
/*** By Raven G. Duran, this is free and open source.. Share to everyone that needs it! :) ***/
int main(){
char name[100][100],temp[100];
int i, j, n;
printf("================================================================\n");
printf(" _ __ _____ __ \n");
printf(" / | / /___ _____ ___ ___ / ___/____ _____/ /____ _____\n");
printf(" / |/ / __ `/ __ `__ \\/ _ \\ \\__ \\/ __ \\/ ___/ __/ _ \\/ ___/\n");
printf(" / /| / /_/ / / / / / / __/ ___/ / /_/ / / / /_/ __/ / \n");
printf("/_/ |_/\\__,_/_/ /_/ /_/\\___/ /____/\\____/_/ \\__/\\___/_/ \n");
printf(" \n");
printf("================================================================\n\n");
// Get the number of names
printf("Enter number of names: ");
scanf("%d", &n);
fflush(stdin);
// Get the names
printf("\n::: Enter the names :::\n");
for (i = 0; i < n; i++){
printf("Name %d: ",i+1);
fgets(name[i],100,stdin);
fflush(stdin);
}
// Sort the names
for (i = 0; i < n - 1 ; 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------------------------------------------\n");
printf("Names in Alphabetical Order\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++){
printf("%s", name[i]);
}
printf("------------------------------------------\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment