C language string around sorting exchange

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 strings by sorting the given conditions, based upon the results of the line after the reordering into an array of strings in xx, after the function call WriteDat () to xx result in output to a file out.dat.

Conditions: From the middle of the string into two, the left part of the character according to the ASCII value of the ascending sort, the left part of the right part of the exchange sorted.

If the length of the original string is odd, the characters do not participate in the intermediate process, the character is still on its original position.

For example: the position 012345678

Source string dcbahgfe 4 3 2 1 9 8 7 6 5

After the process string hgfeabcd 8 7 6 5 9 1 2 3 4

Part of the source file exists in prog1.c.

Do not alter the main function main (), read the contents of the function ReadDat () and write functions WriteDat () of.

#include <stdio.h>

#include <string.h>

#include <conio.h>

char xx[20][80];

void jsSort()

{ int i,j,k,strl,half;

char temp;

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

{ strl=strlen(xx[i]);

half=strl/2;

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

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

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

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

for(j=half-1,k=strl-1;j>=0;j–,k–)

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

}

}

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;

clrscr();

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

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 3171

Guess you like

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