0

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?

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
dutC
  • 1
  • So from your code, it seems like you want to read the lines from a text file, and print out/write back to a file a sorted list of each of the words that appear ? You can store all the words and for simplicity apply bubble sort. – Sudheesh Singanamalla Mar 30 '17 at 05:56

1 Answers1

0

yes that is correct. But i cant find where it goes wrong. First of all. i can't manage the programe van writing the words to a textfile and second i have no idea how to give the program the wright code to look to all the words and not only the words on a new line.

dutC
  • 1