I am supposed to take the contents of 1 text document and output it into another by have the lines alphabetized.
Your program should:
– open the file input.txt
– read all the lines in input.txt into an array of C-strings
– sort the array using bubble sort
– write the resulting sorted array to the file output.txt
You may assume:
– the file input.txt exists and contains at least one line
– input.txt contains less than 10000 lines
– each line in input.txt is less than 200 characters long
If the program is run as follows:
a8 test1.txt test2.out
And test1.txt contains:
zz
xx
yy
cc
ww
aa
Then the file test2.out should contain:
aa
cc
ww
xx
yy
zz
So far I am able to get it to copy to the output file, but now I need to figure out how to alphabetize them.
My code so far:
We are advised to use bubblesort:Code:#include <stdio.h> #include <stdlib.h> #define LINESIZE 200 #define MAXNUMBER 10000 void copyByChar(char * p_inFileName, char * p_outFileName) { FILE * p_fileIn = fopen(p_inFileName, "r" ); FILE * p_fileOut = fopen(p_outFileName, "w" ); char myChar = fgetc(p_fileIn); while (myChar != EOF) { fputc(myChar, p_fileOut ); myChar = fgetc(p_fileIn); } fclose(p_fileIn); fclose(p_fileOut); } int main (int argc, char ** argv) { copyByChar(argv[1], argv[2]); }
However, I'm not sure what steps I should take next to begin alphabetizing each line. I will also have to make sure I read past the first character if both lines start with the same character. I can use strcpy, but I'm not sure how to implement it.Code:void bubbleSort ( unsigned int a[], int len) { int i; int j; for (i = 0; i < len; i++ ) { for (j = len - 1; j > i; j--) { if (a[j-1] > a[j]) { unsigned int tmp = a[j-1]; a[j-1] = a[j]; a[j] = tmp; } } } }
Thanks for any help.



LinkBack URL
About LinkBacks




