Thread: problem with a string sorting function

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

    Smile problem with a string sorting function

    I am trying to a simple string sorting function since I am now learning array and string lesson. however, my function is not working as I expected.
    Could you please help debugging the code below?, I have a problem at the function sort.
    I cannot correctly sort the string passed to the function in order at all.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAXLINES 28
    
    char *lines[MAXLINES];
    
    int get_lines(char *lines[]);
    
     void sort(char *p[], int n)
     {
         int b;
         char *x;
    
             for (b = 0; b < n-1; b++)
             {
                  if (strcmp(p[b], p[b+1]) > 0)
                 {
                      x = p[b];
                      p[b] = p[b+1];
                      p[b+1] = x;
                 }
             }
     }
    main()
    {
    	int number_lines = 0;
    	
    	number_lines = get_lines(lines);
    
    	if ( number_lines < 0 ) 	{
    		printf("\nMemory allocation error!");
    		exit (-1);
    	}
    
    	sort(lines, number_lines);	
    
    	return 0;
    }
    
    int get_lines(char *lines[])
    {
    	int n = 0;
    	char buffer[80]; // temporary storage for each line
    	
    	puts("Enter one line at a time; enter a blank when done.");
    	
    	while ( (n<MAXLINES) && (gets(buffer)!= 0) && (buffer[0]!='\0') )
    	{
    		if ( (lines[n] = (char *) malloc(strlen(buffer)+1))== NULL )
    				return -1;
    				
    		strcpy (lines[n++], buffer);
    		
    	}
    	return n;
    }
    Last edited by whichet; 11-30-2007 at 01:03 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Consider the last string entered. How far can it move in your sort() function? (Bubblesort always takes multiple sweeps through the list to work correctly.)

    And it's int main. (You're returning 0, after all.)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    gets() is a bad idea -- ses the FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Also, exit() is in <stdlib.h>, so you should be including this header 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. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM