我有一个问题,我的程序在C中必须找到带有N个字母的单词,并按字典顺序对它们进行计数并将它们保存到另一个文件中。我怎样才能按字母顺序排列单词?排序字母在C中的排序
这是我的代码:
#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);
}
实现您自己的排序函数 – AkaSh