473,395 Members | 1,978 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,395 software developers and data experts.

Question about Sorting Strings

I am taking C program course and i got some problem about sorting strings, hope someone could help me to solve the problem...

The is the output which suppose to be:
-------------------------
car
house
antidisestablishmentarism
another line of text
C programming is interesting
apple sauce
pumpkin pie
cable TV news
drink of water
yet one more line of text!
1) Print list of strings in the order input.
2) Print list of strings in the reverse order input.
3) Print list of strings in order of length.
4) Quit

-------------------
Enter: 3
Sorted Line #0: C programming is interesting
Sorted Line #1: yet one more line of text!
Sorted Line #2: antidisestablishmentarism
Sorted Line #3: another line of text
Sorted Line #4: drink of water
Sorted Line #5: cable TV news
Sorted Line #6: pumpkin pie
Sorted Line #7: apple sauce
Sorted Line #8: house
Sorted Line #9: car
1) Print list of strings in the order input.
2) Print list of strings in the reverse order input.
3) Print list of strings in order of length.
4) Quit
-----------------------

just want to ask how could i set different quote into different array, so i could compare them?
Nov 22 '11 #1
9 2058
weaknessforcats
9,208 Expert Mod 8TB
The strings are not supposed to be sorted.

What you are to do is print the strings in the order in which they were entered. This has nothing to do with the contents of the string.

So put the strings in an array as you enter them and then just display the array front to end and end to front.

Post again when you have this working. Then we'll talk about displaying by length.
Nov 22 '11 #2
is that mean..
Expand|Select|Wrap|Line Numbers
  1.  
  2. if (option = 1 )
  3. {
  4.  printf("car");
  5.  printf("house ");
  6. }
  7.  
  8. if (option =2)
  9. {
  10.  printf("yet one more line of text!");
  11. }
  12.  
  13.  
since i am not that good at C program...
hope you can explain to me more detail...
Nov 22 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
If you enter these strings:

car
truck
airplane

then in reverse order these are

airplane
truck
car

If these were sorted by value you would have:

airplane
car
truck

But you are not being asked to do this.
Nov 22 '11 #4
in stead of using the simple method like using [printf]

how do i set [car, truck,airplain] into strings?
Nov 22 '11 #5
donbock
2,426 Expert 2GB
Is that original list of strings (car, house, ..., yet one more line of text!) supposed to be typed in by the user or do you think it is ok to "hard-code" that list into your program?

Never mind. I suggest you start by writing functions that accept an array of strings and produce the three kinds of outputs. Whether your main program populates that array from user input or a hard-coded initializer has no impact on the lower level function. Start with a hard-coded initializer so you can focus on the lower level functions. Change it later if you wish.
Expand|Select|Wrap|Line Numbers
  1. static const char * const array[] = {
  2.    "car", "house", ..., "yet another line of text!" };
  3. static const int arraysize = (int) (sizeof(array) / sizeof(array[0]));
Nov 22 '11 #6
johny10151981
1,059 1GB
do you know how to sort a number array??

have you seen the bubble sort??

If you understand bubble sort then ask. I believe you will get help from people over here about how you can effectively use bubble sort to sort your string array. (Bubble sort is the simplest of all sorting)
Nov 22 '11 #7
never heard it before... may be i have learn something similar...
how do i use it to get the out put in order?
Nov 22 '11 #8
donbock
2,426 Expert 2GB
Review the replies from weaknessforcats. Option 1 calls for printing the strings in the order of input; no sorting required. Option 2 calls for printing the strings in the reverse order of input; no sorting required. Option 3 calls for printing the strings in order of length; here you will have to sort.

Get options 1 and 2 working first before you worry about sorting. Where do you stand on those?
Nov 22 '11 #9
this is what i have right now, is it correct?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4.  
  5.  
  6. {
  7. printf("car\n", "house\n", "antidisestablishmentarism\n", "another line of text\n",
  8.        "C programming is interesting\n", "apple sauce\n", "pumpkin pie\n",
  9.         "cable TV news\n", "drink of water\n", "yet one more line of text\n");
  10. printf(" 1) Print list of strings in the order input.\n" , 
  11.        " 2) Print list of strings in the reverse order input.\n",
  12.        " 3) Print list of strings in order of the length\n");
  13. char c=getcher();
  14.  
  15. if ( c == 1)
  16. {
  17. static const char array[] = (
  18.  "car", "house", "antidisestablishmentarism", "oanother line of text", 
  19.  "C programming is interesting", "apple sauce", "pumpkin pie", 
  20.  "cable TV news", "drink of water", "yet one more line of text");
  21.  static const int arraysize = (int) (sizeof(array)/ sizeof(array[0]));
  22. }
  23.  
Nov 22 '11 #10

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

Similar topics

2
by: Shaun | last post by:
Hello! I can't seem to get paths and variables working together: import os a = 'books' os.chdir( '/test') os.mkdir("/test/"a) the last line does not seem to work. os.mkdir(a) makes the...
3
by: amber | last post by:
Hello, Is there a simple way to sort a listbox that contains strings that are numbers? My listbox is populated from a database field that has a datatype of string, but typically contains numbers....
3
by: Blue Man | last post by:
hello I am writing an error handling within different classes in asp.net. the error class reports the Exception E's string to the database. but it cuts big strings, if i just display the error it...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
4
by: Martin Fletcher | last post by:
when I execute dim strTEMP_DATA as string strTEMP_DATA = Encoding.ASCII.GetString(rBuffer).ToString I alway get a string without the closing quote. I see this in the 'Autos' window, I also...
1
by: | last post by:
I'm querying Index Server to return search results, both regular properties and some custom properties I've created. Index Server has this preference for thinking about information as strings...
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "ä", "ü",......
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++...
4
by: Holger | last post by:
I tried to do this elegantly, but did not come up with a good solution Sort strings like foo1bar2 foo10bar10 foo2bar3 foo10bar2 So that they come out: foo1bar2
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.