ALPHABETIZING INPUTS FROM FILE input.... im stuck and getting frustrated.. tried everthing i can think of
this is what i need to sort
XMNP154
ALDS623
KMNT438
PRHU967
HGSE287
LKNM435
DETR178
OPRT348
LRHY412
JKLP693
PLKM563
TSRL923
MNOR789
JKLN610
EMKM579
TSCL623
MNKQ712
both alphabetically and unsorted.. SOMEONE PLEASE HELP
CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSTR 17
void funcinput(int numstr, char *strptr1[ ]);
void funcsort(int numstr, char *strptr1[ ]);
void funcoutput(int numstr, char *strptr[ ], char[ ]);
FILE *inptr, *outptr;
int main()
{
char *strptr[MAXSTR];
int numstr;
scanf("%c", &numstr );
if (numstr > MAXSTR)
numstr = MAXSTR;
funcinput(numstr, strptr);
funcoutput(numstr,strptr, "UNSORTED STRING");
funcsort(numstr, strptr);
funcoutput(numstr, strptr, "SORTED STRING");
return 0;
}
/******************************************************************************/
/* */
/* funinput: input character strings into dynamic string and store */
/* the addresses in a character pointer array */
/******************************************************************************/
/* */
/* Output Parameters: */
/* *strptr1[NUMSTR] character pointer array */
/* */
/* Input Variables: */
/* strname1[STRLNG] character string buffer */
/* */
/******************************************************************************/
void funcinput(int numstr, char *strptr1[ ])
{
int const STRLNG = 80;
int i;
char strname[STRLNG];
FILE *inptr;
inptr = fopen("sortin.dat", "r");
fscanf(inptr, "%c",&i);
for(i = 0; i < numstr; i++)
{
fgets(strname , STRLNG, inptr);
strptr1[i] = (char *)malloc(strlen(strname));
strcpy(strptr1[i], strname);
}
fclose(inptr);
return;
}
/******************************************************************************/
/* */
/* funcsort: sort the character strings */
/* */
/******************************************************************************/
/* */
/* Input Parameters: */
/* strptr1[ ] unsorted character string array pointers */
/* */
/* Computed Variables: */
/* none */
/* */
/* Retruned Parameters: */
/* strptr1[ ] sorted character string array pointers */
/* */
/******************************************************************************/
void funcsort(int numstr, char *strptr1[ ])
{
int i, j;
char *strtemp;
for(i = 0; i < numstr - 1; i++)
{
for (j = i + 1; j < numstr; j++)
{
if(strcmp(strptr1[i], strptr1[j]) > 0)
{
strtemp = strptr1[i];
strptr1[i] = strptr1[j];
strptr1[j] = strtemp;
}
}
}
return;
}
/******************************************************************************/
/* */
/* funcoutput: Output the character strings */
/* */
/******************************************************************************/
/* */
/* Input Parameters: */
/* strptr1[ ] character string arrays */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/******************************************************************************/
void funcoutput(int numstr, char *strptr1[ ], char title[ ])
{
FILE *outptr;
int i;
outptr = fopen("sortout.dat", "w");
fputs(title, outptr);
for(i = 0; i < numstr; i++)
{
fputs(strptr1[i], outptr);
fprintf(outptr,"%c");
}
return;
}