Thread: Sorting paragraphs the other way around

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Unhappy Sorting paragraphs the other way around

    Hi!!
    I have to make a program which will be sorting paragraphs backwards
    like from this
    SOME TEXT
    SOME MORE TEXT
    A HELL LOT MORE OF SOME TEXT WITH TEXT ON TOP OF THE TEXT BEHIND THE TEXT A LOT OF TEXT

    to this

    A HELL LOT MORE OF SOME TEXT WITH TEXT ON TOP OF THE TEXT BEHIND THE TEXT A LOT OF TEXT
    SOME MORE TEXT
    SOME TEXT


    in other words the program finds the \t tab character then reads till the \n character and thats the first paragraph then writes the second paragraph in front the first one and so on till the are no Paragraphs to proccess

    Any ideas? (some code would be nice too)
    thnx.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Rll

    Any ideas? (some code would be nice too)
    thnx.
    Sure, I have an idea. You are lazy.

    Now read the rules - may as well read this too. Then you can put some effort into writing a program, and then if it still does not work, come back here and post your *best* attempt. It would be at that point where someone might want to help you.

    thnx...



    ~/
    Last edited by kermit; 10-30-2005 at 08:01 AM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Rll
    I have to make a program which will be sorting paragraphs
    I would swear that says make a program, not request a program from someone.

    Alright what you want to do is read a string(paragraph) into an array index, then increase the index, and repeat until you read the whole file. Then you want to output the array in reverse index.

    That's my two cents and don't ask me to borrow a dollar.
    ...and let me add I'm not the best programmer, so if you ask for a dollar, I might give it in foreign currency.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Post

    'some' effort

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void read_file ();
    void show_file ();
    void mix();
    FILE *input, *output;
    int d;
    char *line;
    
    main ()
    {
    	if ((input=fopen("test.txt","r"))!=NULL)
    	{
    		read_file();			
    		mix();
    	        int i;
                    for (i=0;i<d;i++)
    		printf ("%c", line[i]);
             }
    	else
    		printf ("File test.txt not found\n");
            printf ("\n");
            system("PAUSE");	
     
    }
    
    void read_file ()
    {
    	int i;
    	d=0;
    	
        while (!feof(input))
    	{
               fgetc(input);
    		   d++;
    	}
    	d--;
    	rewind(input);
    	line=(char*)malloc(sizeof(int)*d);
    	for (i=0;i<d;i++)
    	{
    		line[i]=fgetc(input);
    	}
    	fclose(input);
    	show_file();
    }
    
    void show_file ()
    {
    
    int i;
    		for (i=0;i<d;i++)
    		
    			printf ("%c", line[i]);
    
    }
    
    void mix()
    {
    	int a,i;
        output=fopen("output.txt","w");
      
        for(i=0;i<d;i++)
    	{
        	for(a=0;a<d;a++)
    	    {	
    //OOAI - OUT OF ANY IDEAS
                }
    	
        a=0;
    	}
    fclose(output);
    }

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    How about an unsorted doubly linked list to do your paragraph swapping?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Quote Originally Posted by SlyMaelstrom
    I would swear that says make a program, not request a program from someone.

    Alright what you want to do is read a string(paragraph) into an array index, then increase the index, and repeat until you read the whole file. Then you want to output the array in reverse index.

    That's my two cents and don't ask me to borrow a dollar.
    ...and let me add I'm not the best programmer, so if you ask for a dollar, I might give it in foreign currency.
    THNX, that's what i've been looking for

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't cast malloc().

    This part of main()
    Code:
                    for (i=0;i<d;i++)
    		printf ("%c", line[i]);
    looks like a job for show_file().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM