Thread: Alphabetizing Lines of Text

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    Alphabetizing Lines of Text

    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:

    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]);
    
    }
    We are advised to use bubblesort:
    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;
    			}
    		}
    	}
    }
    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.

    Thanks for any help.
    Last edited by Adrian; 11-30-2007 at 09:31 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use strcmp() to compare strings. It handles the comparison for you so bubblesort (or any other sorting algorithm) will work.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Quote Originally Posted by MacGyver View Post
    Use strcmp() to compare strings. It handles the comparison for you so bubblesort (or any other sorting algorithm) will work.
    Alright, that's what I was expecting to do. How would I get each line of text into an area where I can use the bubblesort process on them? I know each character is being read, but I'm not sure how to recall all those characters and then refer to each line as a separate array of characters. For instance, if I were to make a bubblesort function, where would I call it, and what parameters would I bring up into the function.

    Thanks
    Last edited by Adrian; 11-30-2007 at 09:40 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read each line of the file into a char array, say

    char lines[100][20];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Check for NULL after fopen, because it can fail! If it fails, your app will crash!
    And secondly, since all those characters are sorted by line, I suggest you read the contents line-by-line into a 2D array like Salem suggests. Then you can sort them and print them back.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Alright, a 2d array would make sense, thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  3. Big problems with the Text function
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 05-22-2002, 10:42 AM
  4. How to erase duplicate lines in a text file?
    By miketv in forum C Programming
    Replies: 0
    Last Post: 02-25-2002, 11:18 PM
  5. Text Mode to more than 25 lines
    By karsch1 in forum C Programming
    Replies: 3
    Last Post: 08-31-2001, 01:21 PM