473,390 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 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 8416
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

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

Similar topics

9
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the...
2
by: JasBascom | last post by:
I thought it was too much to put this program in with my last message. The program compiles ok, but when I execute, I get access errors. can someone put it through their compiler please. I think...
1
by: quirdan | last post by:
Hi, I am after some advice about which data structures I should use. I'm developing a program and I am at the point where all the strings are being generated and printed one by one with...
7
by: Ron Adam | last post by:
I have several applications where I want to sort lists in alphabetical order. Most examples of sorting usually sort on the ord() order of the character set as an approximation. But that is not...
4
Cyberdyne
by: Cyberdyne | last post by:
In your All Programs Menu, some of your programs are in alphabetical order and others are not. This makes it very difficult to seek out a program that may be hidden in a maze of program folders and...
1
by: Randeh | last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm...
6
by: Randeh | last post by:
Here's my working code for simple a 2-D alphabetical bubble sort. All I need now is to just make it case insensitive (ignores capitalization) and I've been trying to throw toupper or tolower in...
7
by: canteyn | last post by:
Here is my problem: Structure typedef struct { char lname; char fname; int age; double salary; } employee_t;
3
by: eagerlearner | last post by:
I have the following code, which does not sort the list in alphabetical order, how can I sort everything in alphabetical order ? Before that, I want to ask, when an insertion occur, does it compare...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.