Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an array of strings!! 1

Status
Not open for further replies.

Cbasic

Programmer
Aug 26, 1999
4
US
How do you:-<br>
Sort the words(including numbers and special characters) of a input file in alphabetical order and saves this sorted version in a file called "result.s". Also in limiting the size of the input file to 500 bytes.<br>
<br>
Could I get a detailed explanation in write the program. I am basiclly learning C.
 
Hi<br>
<br>
I'm doing an assignment right now on sorting arrays of intergers. Strings are slightly more complicated in C. Basically they are an array of characters and theres a whole header file for them &lt;string.h&gt;. Also for outputting to a file you'll need fopen(), fclose() and fprintf(). Once the assignment due date has wizzed past me I'll be happy to share the code and poorly explain how it could be adapted for strings.
 
Cbasic,<br>
<br>
Have a look at the book "The C Programming Language"; there are example programs in there that do almost exactly what you want.<br>
<br>
Be warned though - your lecturer has probably heard of this book so I wouldn't just copy the stuff.<br>
<br>
Regards<br>
<br>
Mike<br>
---<br>
Mike_Lacey@Cargill.Com<br>

 
Just a thought<br>
i have done a similar assignement just not as exact on memory size of the output file. The best way would probably to take all the data from the input file put it in an array and sort it from their and then output it. You might have some problems getting the exact 500 bytes and have to look up how each variable will output and how the os will save it. I can help you on the sort if you get this message.<br>
<br>
see you<br>
moses
 
There's a C quick sort function that is a bit of a pain in the bum to use but once you work it out it's really good. I wrote a bit of code for sorting an array of strings that works using qsort(). Unfortunately I wasn't allowed to use it for my assignment.<br>
<br>
I'll post it when i'm at home (its on my computer there)
 
Found it (it took a while)<br>
<br>
<br>
int compare(const void *p, const void *q){<br>
return strcmp(*(char**)p, *(char**)q);<br>
}<br>
<br>
qsort(list, length, sizeof(char*), compare);<br>
<br>
the compare function looks nasty because it needs to take void * arguments. The arguements for qsort are list - a pointer to an array of things (in this case list was char *list[], the same functions could be used for char **list), length - the length of the array of things you may just have to keep track of it as you read in strings, size - the size of each item in the array in bytes (its best to use the sizeof() function because different architectures store them as different sizes) , and finally compare - a function that takes two void* arguements and returns an integer -1 for less than, 0 for equal and 1 for greater than (these can actually be played around with if you want the array sorted in descending order). The return value is an integer (I think) the value indicates success or failure and the array you passed in comes out sorted, or mangled if the compare function is wrong.<br>
<br>
hope it works for you
 
I agree with rotovegasBoy .But it fails ,if you compare a string with &quot;a1&quot; with &quot;a08&quot; . Since you may think that &quot;a1&quot; is equivalent to &quot;a01&quot;. But according to improper formatting it will give the wrong result. The &quot;a08&quot; becomes shorter than &quot;a1&quot;. So you 've to take care of that.<br>
Does it answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A><br>

 
I want to create a string using strcat, problem is it will contain the ampersand character. How do you handle special characters like this using the string manipulation functions?

Here is my code that is failing on the last line with a memory access violation (I think because & is used in de-referencing pointers):

if (count > 0){//create string to accept AGs
s1= &quot;AcceptStatus0=Y&quot;;
lr_output_message(&quot;s1 is: %s&quot;, s1);
s2 = &quot;&AcceptStatus&quot;;
lr_output_message(&quot;s2 is: %s&quot;, s2);//OK so far
strcat(s1,s2);//fails here
}

 
see my answer to post &quot;alternatives&quot;, espeically point on concatenation where destination string needs to be big enough to hold both strings. Hoping to get certified..in C programming.
 
Thanks, bigtamscot! I will look at this, but was hoping for an easier way than creating a new function.

Oops, I meant to start this as a new thread. [blush] Anyone else who has an answer, please reply under the other thread. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top