I already have this code and I need help figuring out how to take in a .txt file and printing it out in alphabetical order. I dont understand how to use the strcmp function. Please help!
Code:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 40
void sort(char names[][MAX_LENGTH], int size);
void print(char names[][MAX_LENGTH], int size);
int my_strcmp(const char* s1, const char* s2);
void swap(char names[][MAX_LENGTH], int i, int j);
void my_strcpy(char* s1, const char* s2);
int main() {
char filename[MAX_LENGTH];
FILE *fin;
int i;
// Get the name of the file and open it.
printf("What file stores the input names?\n");
scanf("%s", filename);
fin = fopen(filename,"r");
// Find out the number of names.
int n;
fscanf(fin, "%d", &n);
char names[n][MAX_LENGTH];
// Read in all of the names.
for (i=0; i<n; i++)
fscanf(fin, "%s", names[i]);
sort(names, n);
printf("Here is the list of names:\n");
print(names, n);
fclose(fin);
system("PAUSE");
return 0;
}
void sort(char names[][MAX_LENGTH], int size) {
int i,j;
// How far we have to go in the array on each iteration.
for (i=size-1; i>0; i--)
// Just swap any adjacent elements that are out of order.
for (j=0; j<i; j++)
if (my_strcmp(names[j], names[j+1]) > 0)
swap(names, j, j+1);
}
void swap(char names[][MAX_LENGTH], int i, int j) {
char temp[MAX_LENGTH];
my_strcpy(temp, names[i]);
my_strcpy(names[i], names[j]);
my_strcpy(names[j], temp);
}
void print(char names[][MAX_LENGTH], int size) {
int i;
for (i=0; i<size; i++)
printf("%s\n", names[i]);
}
int my_strcmp(const char* s1, const char* s2) {
}
void my_strcpy(char* s1, const char* s2) {
int i;
for ( i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) {
;
}
}