C language characters sort

ReadDat function () read from the file in.dat achieved stored data lines 20 to the string array xx (length of strings to less than 80).

Please prepare function jsSort (), whose function is the function: in units of index string variable is odd character ASCII value of their ascending sort order, based upon the results sorted into character line again xx string array, the WriteDat after the function call () to result in out.dat xx output to a file.

For example: position 01234567

Source string abcdefgh

After processing the string ahcfedgb

Part of the source file exists in prog1.c.

Do not modify the main function main (), the content data read function ReadDat () function and output data WriteDat () of.

#include <stdio.h>

#include <string.h>

#include <conio.h>

char xx[20][80];

void jsSort()

{int i,j,k,strl;

char ch; for(i=0;i<20;i++)

{ strl=strlen(xx[i]);

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

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

if(xx[i][j]>xx[i][k]) { ch=xx[i][j];xx[i][j]=xx[i][k];xx[i][k]=ch;}

}

}

void main()

{

readDat();

jsSort ();

writeDat();

}

readDat()

{ FILE *in;

int i=0;

char *p;

in=fopen(“in.dat”,“r”);

while(i<20&&fgets(xx[i],80,in)!=NULL){

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

if§*p=0;

i++;

}

fclose(in);

}

writeDat()

{

FILE *out;

int i;

out=fopen(“out.dat”,“w”);

clrscr();

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

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

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

}

fclose(out);

}

Published 239 original articles · won praise 3 · Views 3170

Guess you like

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