471,892 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

Alphabetical sort using strcmp question

I had to write a program that would accept 7 strings through scanf, list the strings, alphabetize, and relist. I was supposed to use strcmp to assist with the sort and write it so that it can sort on a different number by only changing the N_STRINGS 7. So, I've written the program and of course it's not working I admit that the pointers to strings and arrays confuse me. Can someone point me in the right direction? I would appreciate any help!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXLINE 50
  4. #define N_STRINGS 7
  5.  
  6. char *change(const char *s);
  7. int main (void)
  8. {
  9.     char list[MAXLINE];
  10.     const char *m;
  11.     int k;
  12.     int i;
  13.     int j;
  14.  
  15.  
  16.  
  17.     printf("\nEnter 7 strings that you would like sorted alphabetically.\n\n");
  18.  
  19.     scanf("%49[ a-zA-Z]", &list);
  20.  
  21.     printf("\nThe following %d strings will be sorted alphabetically:\n\n", N_STRINGS);
  22.  
  23.     for (k = 0; k < 1; ++k)
  24.  
  25.         printf("%s\n\n", change(list));
  26.  
  27.     for (i = 0; i < MAXLINE - 1; ++i)
  28.     {
  29.         for (j = i; j < MAXLINE; ++j)
  30.         {
  31.             if (strcmp(m[i], m[j]) > 0)
  32.             {
  33.                 const char *swap = m[i];
  34.                 m[i] = m[j];
  35.                 m[j] = swap;
  36.             }
  37.         }
  38.     }
  39.  
  40.  
  41.     printf("The alphabetical listing is:\n");
  42.     for (k = 0; k = N_STRINGS; ++k)
  43.         printf("%s", m[i]);
  44.  
  45. }
  46. THIS IS THE CHAR *CHANGE FUNCTION.
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. #define MAXLINE 50
  51. #define N_STRINGS 7
  52.  
  53. char *change(const char *s)
  54. {
  55.     static char newstring[MAXLINE];
  56.     char *p = newstring;
  57.  
  58.     *p++ = '\t';
  59.     for ( ; *s != '\0'; ++s)
  60.         if (*s == ' ')
  61.         {
  62.             *p++ = '\n';
  63.             *p++ = '\t';
  64.         }
  65.         else 
  66.             *p++ = *s;
  67.     *p = '\0';
  68.     return newstring;
  69. }
  70.  
Nov 20 '07 #1
1 8133
RRick
463 Expert 256MB
Sorry, but I only have time right now to point out problems, and not describe their solutions in detail.

One mistake is in your understanding of how C deals with strings and lists of strings. Your string lists need to be changed.
Expand|Select|Wrap|Line Numbers
  1. char * string; // Just room for a pointer, but has an bad pointer value;
  2. char string2[10]; // Defines a string that can hold 9 chars, but nothing is assigned.
  3. char **list; // A list of strings
  4. char list2[MAXLINE][7]; // Room for 7 strings of MAXLINE size. Please help if this is backwards
  5.  
It looks like you are implementing a bubble sort and this is pushing the sorted value to the bottom. Your outer loop is working from the top down and instead needs to work from the bottom up.

Your change routine is returning a local address. You need to malloc the string and return that. If you do, it looks like you'll have a memory leak.
Nov 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by JasBascom | last post: by
2 posts views Thread by JasBascom | last post: by
7 posts views Thread by Ron Adam | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.