C language string sorting

Function ReadDat () to achieve reading an article in English from the file stored in the string array IN.DAT xx Please prepare function SortCharD (), whose function is the function: in units of characters descending sorted order, based upon the results of the sorted row xx is reentered into the string array, after the function call writeDat () to result in OUT2.DAT xx output to a file.
Example: Original: DAE, BFC
CCbbAA
results: FEDCBA

      bbCCAA

Format of the original data files are: the width of each row is less than 80 characters, including punctuation and spaces.
#include <stdio.h>

#include <string.h>

#include <conio.h>

char xx[50][80];

int maxline = 0; / total number of rows of articles /

int ReadDat(void);

void WriteDat(void);

void SortCharD(void)

{int i,j,k,strl;

char ch;

for(i=0;i<maxline;i++)

{strl=strlen(xx[i]);

for(j=0;j<strl-1;j++)

for(k=j+1;k<strl;k++)

if(xx[i][j]<xx[i][k])

{ch=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=ch;}

}

}

void main()

{

clrscr();

if(ReadDat()){

printf ( "IN.DAT data file can not be opened \ n \ 007!");

return;

}

SortCharD();

WriteDat();

}

int ReadDat(void)

{

FILE *fp;

int i=0;

char *p;

if((fp=fopen(“IN.DAT”,“r”))==NULL) return 1;

while(fgets(xx[i],80,fp)!=NULL){

p=strchr(xx[i],’\n’);

if§*p=0;

i++;

}

maxline=i;

fclose(fp);

return 0;

}

void WriteDat(void)

{

FILE *fp;

int i;

fp=fopen(“OUT2.DAT”,“w”);

for(i=0;i<maxline;i++){

printf("%s\n",xx[i]);

fprintf(fp,"%s\n",xx[i]);

}

fclose(fp);

}

Published 239 original articles · won praise 3 · Views 3150

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105176877