Thread: Ordering strings in an array by length

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Ordering strings in an array by length

    So for this program I have to have the user input 10 strings. Then the menu asks them how they would like their strings outputted. The first two options (which are print the way they were input, and print in reverse order from how they were input) I have working properly. It's the third option that is giving me trouble.

    I need to print their strings descending from longest to shortest.
    i.e. if they input:
    aaa
    b
    cccccc
    dd

    it will output:
    cccccc
    aaa
    dd
    b

    The function I am using to do this is incorrect and I'm a bit lost now.

    Here is my code:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    #include<stdlib.h>
    
    _Bool readLine(char *str, int maxLen);
    int array_compare(const void *a, const void *b);
    
    int main ()
    {
      char strs[10][80];
      int numStrs=0;
      int choice;
      int i;
      
      for (i=0; i<10; ++i)
      {
      readLine(strs[i], 80);
      }
      
        do
        {
        printf("1) Print list of strings in the order input.\n");
        printf("2) Print list of strings in the reverse order input.\n");
        printf("3) Print list of strings in order of length.\n");
        printf("4) Quit\n");
        scanf("%d", &choice);
    	
    	  if(choice==1) //print strings in order of input
    	  {
    	    for (i=0; i<10; ++i)
    	    printf("%s", strs[i]);
    	  
    	  printf("\n");
    	  }
    	
    	  if(choice==2) //print strings in reverse order of input
    	  {
    	    for (i=9; i>=0; --i)
    	    printf("%s", strs[i]);
    	  
    	  printf("\n");
    	  }
    	
    	  if(choice==3) //print strings in order of length
    	  {
          qsort(strs, 10, sizeof(char), array_compare);
    	    for(i=0; i<10; ++i)
    	    printf("%s", strs[i]);
    	
    	  printf("\n");
          }	
        }
    
        while(choice!=4 || choice <=0);
    	
    
    }
    
    _Bool readLine(char *str, int maxLen) //read in strings
    {
    int i=0;
    int c = 'a';
      for(i=0; c!='\n' && i<maxLen-1; ++i)
      {
      c= getchar();
        if(c==EOF)
         break;
    	 
    	 str[i]=c;
      }
      str[i]='\0';
    }
    
    int array_compare(const void *a, const void *b) //order strings by length
    {
    const int *pa = (const int*)a;
    const int *pb = (const int*)b;
    
    return *pa - *pb;
    }
    If anyone can show me how my function should look I would really appreciate it!! Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Oh, and I know there are variables I didn't use, sorry for any confusion regarding that.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use a regular sorter for it, just change the comparison to:
    Code:
    char temp[biggerThanAnyStringYouHave];
    
    for(i=0;i<numStrings-1;i++) {
       for(j=i+1;j<numString;j++) {
          if((strlen(str[i]) > strlen(str[j])) {
            // swap the order of the strings using a temp string, and strcpy or memset
          }
       }
    }
    Last edited by Adak; 11-22-2011 at 06:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ordering an array in a 'strange' order.
    By Ziden in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2010, 09:21 PM
  2. Variable length strings
    By MTK in forum C Programming
    Replies: 9
    Last Post: 08-30-2009, 06:23 PM
  3. Getting confused with strings and length
    By FreeCare in forum C Programming
    Replies: 15
    Last Post: 10-24-2008, 02:02 PM
  4. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM
  5. variable length records for strings
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 02-08-2002, 07:49 PM