473,659 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alphabetical sort using strcmp question

12 New Member
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 8469
RRick
463 Recognized Expert Contributor
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
13098
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 top as it lower in numerical order.
2
2111
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 the problem lies when i declare union Allrecords h1,h2,h3. and then use them to toupper record_type. the debug when i use it can not go past the switch statement. thank you for you help
1
2472
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 loops/recursion using printf("%s\n", my_string);. Instead of printing them, I need to sort them in alphabetical order, and record the amount of times a unique string appears. i.e it will print "ABCD 4" because ABCD appeared 4 times. "BCC 1" because BCC...
7
2707
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 always what you want. The solution of converting everything to lowercase or uppercase is closer, but it would be nice if capitalized words come before lowercase words of the same spellings. And I suspect ord() order may not be correct for some...
4
5530
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 files. The solution is simple: have the computer re-sort the menu. When you install new programs, Windows XP tacks them to the end of your All Programs menu, rather than inserting them in the correct alphabetical order. So every so often you have...
1
5268
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 sure there's more errors but I'll figure those out if I can just get it to compile. #include <iostream> #include <cstring> using namespace std; const int MaxNames = 21; const int MaxChars = 16;
6
4910
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 there but it never seems to work properly. Any ideas? void SortArray(char Array, int pass) { bool swap; int temp; do { swap=false;
7
4418
by: canteyn | last post by:
Here is my problem: Structure typedef struct { char lname; char fname; int age; double salary; } employee_t;
3
3435
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 the string that I am inserting with every string inside the set list ? or just with string that is beside the location that is going to be inserted ? Thank you. #include <iostream> #include <hash_set> #include <string> using namespace std; ...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.