I need to make a program that can sort the words according the alphabet from a to z. the current program I made is capable of sorting the words but only if there on a new line ('\n').
The words that are further in the line are not sorted.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define SIZE 30
#define LEN 31
void bron (char *fnamer)
{
printf("\nGeef de naam van de in te lezen txt file: \n");
gets(fnamer);
//getchar(fnamer);
}
void uitgave (char *fnamer2)
{
printf("\nGeef de naam van de nieuwe geschreven txt file: \n");
gets(fnamer2);
//getchar(fnamer2);
}
int main()
{
char name[LEN][SIZE];
char hold[LEN] ;
int i,j ;
int last ;
FILE *fp;
FILE *fp2;
char ch;
char fnamer[1000];
char fnamer2[1000];
bron(fnamer);
fp=fopen(fnamer,"r");
if(fp==NULL)
{
printf("Error!");
exit(1);
}
uitgave(fnamer2);
fp2=fopen(fnamer2,"w");
if(fp2==NULL)
{
printf("Error!");
exit(1);
}
else
{
for(i = 0 ; !feof(fp) ; i++ )
{
fgets( name[i] , LEN, fp);
}
last = i - 1 ;
fclose(fp);
for (i = last ; i > 0 ; i--)
for (j = 1 ; j <= i ; j++)
if (strcmp(name[j],name[j - 1]) < 0)
{
strcpy(hold,name[j]) ;
strcpy(name[j],name[j - 1]) ;
strcpy(name[j - 1],hold) ;
}
for (i = 0 ; i <= last ; i++)
{
printf("%s",name[i]) ;
// fprintf("%s", name [i]); herzien
}
}
printf("\n%s \t%s",fnamer,fnamer2) ;
//fprintf(fp, %s) ; // herzien
fclose(fp2);
return 0;
}
What is the problem?