I have a problem, my program in C have to find words with N letters, count them and sort them in lexicographical order and save them to an another file. How could I sort the words in alphabetical order?
this is my code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stddef.h>
#include <string.h>
int main()
{
FILE *r, *fp;
char ch[100],t[100];
int n,i,j,x=0;
r=fopen("text.txt","r");
fp=fopen("text2.txt","w");
fgets(ch, 100, r);
char *start;
int len;
char *s = ch;
char *p = s;
printf("Give the length of word: ");
scanf("%d",&n);
printf("\n\nWords with %d letters: \n\n",n);
while (*p) {
while (*p && isspace(*p))
++p;
start = p;
while (*p && !isspace(*p))
++p;
len = p - start;
if (len == n) {
printf("%.*s\n", len, start);
x++;
fprintf(fp,"%.*s",len, start);
}
}
printf("\nNumber of words: %d ",x);
fclose(fp);
getch();
fclose(r);
}