strings, sorting

This is a discussion on strings, sorting within the C Programming forums, part of the General Programming Boards category; Hi, guys, I've been working on sorting an array of strings... It is a work in progress, I did not ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    strings, sorting

    Hi, guys,
    I've been working on sorting an array of strings... It is a work in progress, I did not take into consideration blank spaces, uppercase or lowercase characters as of yet. I need to get thi sfunction into a working condition to work on details.
    When I compile it (no warnings or erros at all, gcc) and run the program, it finishes. So I take a look in my sorted array file and I see just blank lines.
    Can anybody tell me what happened?
    Code:
    void InsertionSort(char *arr[])
    {
    	char *helper = "......................";
    	int i, j;
    
    
      for(i = 1; i < (int)strlen(arr[i])-1; i++) {
    	strcpy(helper, arr[i]);
    	j = i;
    	while(j >= 1 && (strcmp(helper, arr[j-1])<0)) {
    		strcpy(arr[j], arr[j-1]);
    		j -= 1;
    	}
    	strcpy(arr[j], helper);
      }
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I can give you a hint:
    Code:
    i < (int)strlen(arr[i])-1;
    will not tell you how many strings there are in the array of strings.
    You will have to pass that information to InsertionSort()
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Code:
    	char *helper = "......................";
    	strcpy(helper, arr[i]);

    No no no no no no no.

    char *helper gives you a pointer to a static string and you really shouldn't try to edit it at all. Use this instead:
    Code:
      char helper[] = "......................";
    Last edited by Brian; 02-17-2006 at 12:05 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    I tried to correct my code the way you suggested but still gave me the same empty file (empty meaning blank lines, exactly as amny as there should be). I am re-writing my code from scratch now, I'll post again if I have questions...
    Thanks.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Did you take ZuK's hint?
    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 strings
    By sureshhewa in forum C Programming
    Replies: 8
    Last Post: 07-30-2008, 10:58 AM
  2. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  3. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21