By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

K&R C and strings

P: n/a
I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?

A file called Realm List.html contains the following data:
Bladefist-Horde
Nordrassil-Horde
Draenor-Alliance
Nordrassil-Alliance
Nordrassil-Neutral
Draenor-Horde
Moonglade-Horde
Shadowmoon-Horde
Moonglade-Alliance

I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.

Right now I can open the file, I can use strtok to split each line but
I can't work out how to create an array of these strings?

FILE *realmList;
if((realmList=fopen("Realm List.html","w+")) == NULL)
{
printf("Couldn't open \"Realm List.html\" for output\n");
return 1;
}

/*
List Realms and count of each
*/
char strLine[244];

while (fgets(strLine, 244, realmList))
{

char *tmp = NULL;
char token[] = "-";
tmp = strtok(strLine, token);

printf("%s\n", tmp);
/* stuck at this point */
}

Sep 24 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
pkirk25 schrieb:
I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?

A file called Realm List.html contains the following data:
Bladefist-Horde
Nordrassil-Horde
Draenor-Alliance
Nordrassil-Alliance
Nordrassil-Neutral
Draenor-Horde
Moonglade-Horde
Shadowmoon-Horde
Moonglade-Alliance

I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

I'm committed to using ANSI C
Okay. Note that this may not be the "best" or "easiest" language
for this task.
with the K&R book as my guide. It says a
string is an array of chars.
Ah, read more closely. An array of char only contains a string if
it contains at least one '\0' character, i.e.
char a[] = {'h', 'e', 'l', 'l', 'o'};
is not a string while
char a[] = {'h', 'e', 'l', 'l', '\0'};
or, equivalently,
char a[] = "hell";
is.
char a[] = {'h', '\0', 'l', 'l', '\0'};
also contains a string ("h");
Right now I can open the file, I can use strtok to split each line but
I can't work out how to create an array of these strings?

FILE *realmList;
if((realmList=fopen("Realm List.html","w+")) == NULL)
Note: This effectively deletes the contents of "Realm List.html".
You probably want "r+" or just "r" as you stated that you want to
_read_ your input data from the file.
{
printf("Couldn't open \"Realm List.html\" for output\n");
return 1;
}

/*
List Realms and count of each
*/
char strLine[244];

while (fgets(strLine, 244, realmList))
Note that this approach has problems:
- fgets() does not necessarily give you the whole line but only
in this case up to 244-2 non-'\n' characters.
- You still need some "new" storage to put the read string for
a while.
Have a look at C.B. Falconer's fggets() which gives you full lines
and allocates the storage for them.
http://cbfalconer.home.att.net/download/
{

char *tmp = NULL;
char token[] = "-";
tmp = strtok(strLine, token);
This effectively destroys your input, i.e. you cannot sort according
to the "-Alliance" or "-Horde" suffix.
You could alternately find the first '-' with strchr() and store that
position.
printf("%s\n", tmp);
If you just have the position from strchr(), you can use the
precision field to print only a certain number of characters from
a string.
printf(".*s\n", numChars, strLine);

You want an array of variably sized strings, so, having an
array of pointers to the first characters of strings is a better
solution.
Moreover, you do not know the size of the array beforehand, so
using allocated storage may be the best solution.
Before the loop:
size_t size = YOUR_START_SIZE;
size_t count = 0;
char **Strings;
char *strLine;
int ErrOrEOF;
....

Strings = malloc((size+1) * sizeof *Strings);
if (NULL == Strings) {
/* your error handling here */
}

The loop:

while (!(ErrOrEOF = fggets(&strLine, realmList))) {
if (count >= size) {
char **tmp = realloc(Strings, (2*size+1) * sizeof *Strings);
if (NULL == tmp) {
/* your error handling here */
}
Strings = tmp;
size *= 2;
}
Strings[count++] = strLine;
}
if (EOF != ErrOrEOF) {
/* An error occurred */
}

The above is not tested.
As soon as you got it to compile and work, you have "count" strings.
You can sort them using qsort().

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 24 '06 #2

P: n/a
On 24 Sep 2006 11:43:21 -0700, "pkirk25" <pa*****@kirks.netwrote:
>I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef strdup
char *strdup(const char *s) {
char *r;
return (r=calloc(strlen(s)+1,1))?strcpy(r,s):0;
}
#endif

char *read_str(char *s, int max) {
char *r=fgets(s, max, stdin);
if (r) {
int len=strlen(s);
if (len && s[len-1]=='\n')
s[len-1]='\0';
}
return r;
}

typedef struct node_tag {
char *name;
struct node_tag *next;
} node_t, *pnode_t;

pnode_t node_new(const char *n) {
pnode_t i=malloc(sizeof *i);
if (i) {
i->next=0;
i->name=strdup(n);
}
return i;
}

void node_free(pnode_t self) {
if (self) {
if (self->name) free(self->name);
free(self);
}
}

void list_print(pnode_t head) {
if (head) {
puts(head->name);
list_print(head->next);
}
else printf("(end of the list)\n");
}

void list_free(pnode_t head) {
if (head) {
list_free(head->next);
node_free(head);
}
}

void list_insert(pnode_t *phead, pnode_t new_node) {
if (!*phead)
*phead=new_node;
else
if (strcmp(new_node->name, (*phead)->name)<=0) {
new_node->next=*phead;
*phead=new_node;
}
else
list_insert(&(*phead)->next, new_node);
}

int main() {
pnode_t head=0;

list_insert(&head, node_new("Bladefist-Horde"));
list_insert(&head, node_new("Nordrassil-Horde"));
list_insert(&head, node_new("Draenor-Alliance"));
list_insert(&head, node_new("Nordrassil-Alliance"));
list_insert(&head, node_new("Nordrassil-Neutral"));
list_insert(&head, node_new("Draenor-Horde"));
list_insert(&head, node_new("Moonglade-Horde"));
list_insert(&head, node_new("Shadowmoon-Horde"));
list_insert(&head, node_new("Moonglade-Alliance"));

list_print(head);
list_free(head);

return 0;
}
Sep 24 '06 #3

P: n/a
Two great answers.

Thanks - if I can make it all fit together I may well become the first
World of Warcraft gold millionaire!

Sep 24 '06 #4

P: n/a
On Mon, 25 Sep 2006 00:18:20 +0200, Andrea Laforgia
<a.********@andrealaforgia.it.invalidwrote:
>On 24 Sep 2006 11:43:21 -0700, "pkirk25" <pa*****@kirks.netwrote:
>>I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef strdup
This will not prevent this code from compiling when strdup is declared
in one of these headers as something other than a macro. Since it is
not a standard function or standard macro, it should not be in any of
these headers anyway.
>char *strdup(const char *s) {
Function names beginning with str and a lower case letter are
reserved.
char *r;
return (r=calloc(strlen(s)+1,1))?strcpy(r,s):0;
Is there a reason you chose to use calloc and spend the cycles setting
each byte to '\0' when you immediately use strcpy to initialize every
byte anyway?
>}
#endif

char *read_str(char *s, int max) {
char *r=fgets(s, max, stdin);
if (r) {
int len=strlen(s);
if (len && s[len-1]=='\n')
If r is not NULL, can len ever by zero?
s[len-1]='\0';
}
return r;
}

typedef struct node_tag {
char *name;
struct node_tag *next;
} node_t, *pnode_t;

pnode_t node_new(const char *n) {
pnode_t i=malloc(sizeof *i);
if (i) {
i->next=0;
i->name=strdup(n);
}
return i;
}

void node_free(pnode_t self) {
if (self) {
if (self->name) free(self->name);
free(self);
}
}

void list_print(pnode_t head) {
if (head) {
puts(head->name);
list_print(head->next);
Recursion is unnecessarily expensive here. A simple while loop will
work.
}
else printf("(end of the list)\n");
}

void list_free(pnode_t head) {
if (head) {
list_free(head->next);
node_free(head);
}
}

void list_insert(pnode_t *phead, pnode_t new_node) {
if (!*phead)
*phead=new_node;
else
if (strcmp(new_node->name, (*phead)->name)<=0) {
new_node->next=*phead;
*phead=new_node;
}
else
list_insert(&(*phead)->next, new_node);
Recursion can also be avoided here.
>}

int main() {
pnode_t head=0;

list_insert(&head, node_new("Bladefist-Horde"));
list_insert(&head, node_new("Nordrassil-Horde"));
list_insert(&head, node_new("Draenor-Alliance"));
list_insert(&head, node_new("Nordrassil-Alliance"));
list_insert(&head, node_new("Nordrassil-Neutral"));
list_insert(&head, node_new("Draenor-Horde"));
list_insert(&head, node_new("Moonglade-Horde"));
list_insert(&head, node_new("Shadowmoon-Horde"));
list_insert(&head, node_new("Moonglade-Alliance"));

list_print(head);
list_free(head);

return 0;
}

Remove del for email
Sep 25 '06 #5

P: n/a
On Sun, 24 Sep 2006 17:18:12 -0700, Barry Schwarz <sc******@doezl.net>
wrote:
>Is there a reason you chose to use calloc and spend the cycles setting
each byte to '\0' when you immediately use strcpy to initialize every
byte anyway?
Well it comes from my implementation of strndup() which doesn't
necessarily append a '\0' to the new string. However, you're right:
in this case a simple malloc() is enough.
> int len=strlen(s);
if (len && s[len-1]=='\n')

If r is not NULL, can len ever by zero?
Of course: strlen("").
>Recursion is unnecessarily expensive here. A simple while loop will
work.
I know, but I like recursion really much ("De gustibus non disputandum
est" ;-), I feel comfortable with it and, in order to write code
quickly, I use it. The OP can transform that recursion into a plain
iteration. It is just a sample code.
Sep 25 '06 #6

P: n/a
pkirk25 wrote:
I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?

A file called Realm List.html contains the following data:
Bladefist-Horde
Nordrassil-Horde
Draenor-Alliance
Nordrassil-Alliance
Nordrassil-Neutral
Draenor-Horde
Moonglade-Horde
Shadowmoon-Horde
Moonglade-Alliance
This is not a typical format for a .html file. It looks like straight
text.
I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde
So you want to sort the lines of text alphabetically right?
I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.
In straight C strings are just a rats nest of pain and suffering. C
handles string within arrays of chars, however, it doesn't do a lot to
help you manage the array of chars to begin with. I.e., the problems
of memory management are basically yours to solve.
Right now I can open the file, I can use strtok to split each line but
I can't work out how to create an array of these strings?
If you were looking at a language like Perl or Python this kind of
thing is just 4 or 5 lines of code. What you want is some way to do
something similar in C.

By using more advanced strings (i.e., not the crap that the language
comes with), this is possible in the C language. I have written a
string library called "The Better String Library" which is perfectly
suited for this. Using this library leads you to:

#include <stdio.h>
#include <stdlib.h>
#include "bstrlib.h"

/* The string comparison function for qsort */
static int lineCmp (const bstring * s0, const bstring * s1) {
return bstrcmp (*s0, *s1); /* Deal with the extra indirection */
}

int main () {
FILE * fp = fopen ("Realm List.html", "r");
if (fp) {
bstring b = bread ((bNread) fread, fp); /* Read the whole input */
struct bstrList * sl = bsplit (b, '\n'); /* Build the array of
lines */
fclose (fp);
if (sl) {
qsort (sl->entry, sl->qty, sizeof (bstring), lineCmp); /* Sort
the lines */
fp = fopen ("Realm List.html", "w"); /* Overwrite the input file
*/
if (fp) {
int i;
for (i=0; i < sl->qty; i++) { /* Write each line */
fprintf (fp, "%s\n", (char *) sl->entry[i]->data);
}
fclose (fp); /* Commit the file */
}
bstrListDestroy (sl); /* Clean up string list */
}
bdestroy (b); /* Clean up input string */
}
return 0;
}

Not quite as compact as you can get from other modern languages, but a
whole hell of a lot better than what you can achieve in straight C.
Notice how there are no mysterious constants on array declarations that
usually either over-commits memory or under allocates it. You can
download the library here:

http://bstring.sf.net/
FILE *realmList;
if((realmList=fopen("Realm List.html","w+")) == NULL)
"w+" mode means append to current file in write mode. So you will end
up just increasing the size of the input file, rather than overwriting
it with its sorted contents.
{
printf("Couldn't open \"Realm List.html\" for output\n");
return 1;
}

/*
List Realms and count of each
*/

char strLine[244];
Where does the number 244 come from? (Hint: you don't know, and
neither do I, nor does K&R nor anyone else.)
while (fgets(strLine, 244, realmList))
{

char *tmp = NULL;
char token[] = "-";
tmp = strtok(strLine, token);
This tokenization is not useful. Lines are split by '\n', however this
is already taken care of by the fgets(). With lexical sorting, the
hyphened format of your input data is intrinsically taken into account.

Your concern here is to *store* the line somewhere, not parse it. The
problem is that as you run through the loop, you are going to overwrite
the contents of strLine each time -- i.e., you can only hold storage
for one line at a time. What you need to is to create new storage for
each line, and retain a copy of each line in that storage. You then
have the additional problem that you need to hold the entire collection
of allocated lines in such a way as to come back and be able to sort
them.

C has a function for sorting called "qsort" but that requires that your
lines be stuffed in an array. Perhaps we can have some more "magical
numbers":

char * lineArray[23726]; /* I literally mashed the keyboard for that
number */

then somewhere along that way we could use a counter to store the
results of each read -- but we still need storage for each line. So we
learn about "malloc" and we can try to cobble together a solution:

char strLine[244], * lineArray[23726];
int counter = 0;

if (NULL != (realmList = fopen ("Realm List.html", "r"))) {
while (fgets(strLine, 244, realmList)) {
char * storage = (char *) malloc (1+strlen (strLine));
if (storage) {
strcpy (storage, strLine);
lineArray[counter] = storage; /* This may overflow */
counter++;
} else {
printf ("We ran out of memory\n");
return 1;
}
}
fclose (realmList);
qsort (lineArray, counter, sizeof (char *), pstrcmp);

/* Write the file ... */

/* Free the memory (if you want to be a good citizen) */
}

And you are going to have to make a pstrcmp() function which takes a
pair of char **'s and calls strcmp on the once dereferenced parameters.
The problem with these kinds of typical C solutions is that you are
limited to lines of length at most 243 (before truncation and a
resulting input corruption happens), and you can hold at most 23726
lines (before the program overflows and causes undefined behaviour).
So all that work, and still the solution is just not satisfactory. (We
could avoid the overflow by putting a test on the value of counter as
it is incremented and just break out of the loop -- but this just leads
to another truncation condition.)

The solution that uses Bstrlib given above has no such limitation --
you are only limited by available memory. The Bstrlib version will
never give you erroneous results -- errors (in Bstrlib running out of
memory is the only real error) are deterministically detectable and you
can abort the whole process as the errors are detected, or use some
other strategy to proceed if you like.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #7

P: n/a
we******@gmail.com said:
pkirk25 wrote:
<snip>
>I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

So you want to sort the lines of text alphabetically right?
>I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.
Close. It's a sequence of adjacent characters (typically stored in an array,
yes) that is terminated by the first null character in that sequence.
In straight C strings are just a rats nest of pain and suffering.
For websnarf, this may well be true. Other people seem to manage, though.

<snip>
By using more advanced strings (i.e., not the crap that the language
comes with),
Not everyone shares websnarf's opinion.
this is possible in the C language.
It's possible using the ordinary C string library too.
I have written a
string library called "The Better String Library" which is perfectly
suited for this.
Of the two URLs in your sig, one failed to load at all, and the other
presented a bizarre set of cartoon drawings, none of which had any obvious
relation to any kind of string libraries. Perhaps you would be so kind as
to provide a link, so that we can see whether your library meets the OP's
requirement of ANSI C.
You can
download the library here:

http://bstring.sf.net/
Yeah, I tried that one.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #8

P: n/a
Richard Heathfield wrote:
we******@gmail.com said:
pkirk25 wrote:
<snip>
I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde
So you want to sort the lines of text alphabetically right?
I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.

Close. It's a sequence of adjacent characters (typically stored in an array,
yes) that is terminated by the first null character in that sequence.
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.
Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work. I have higher standards for *managing*
so, yes its true for me.
<snip>
By using more advanced strings (i.e., not the crap that the language
comes with),

Not everyone shares websnarf's opinion.
As is in full evidenced by the number of bug-free complete solutions
given here using pure C. *Sound of crickets*.
this is possible in the C language.

It's possible using the ordinary C string library too.
We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something. Even after Andrea Laforgia's
tour de force effort, all he's done is read and print the list of
strings. He's missing the sort part. I mean, link lists are awesome
and all, but how is he planning on sorting it? Does he have a linked
list based merge sort handy?

I mean I have better things to do than to painstakingly debug some code
that I just intend to post to c.l.c. So what motivated me to write up
a whole Bstrlib solution? Obviously, its because its completely
trivial to do using Bstrlib -- its just not much of an effort at all.
I have written a
string library called "The Better String Library" which is perfectly
suited for this.

Of the two URLs in your sig, one failed to load at all, and the other
presented a bizarre set of cartoon drawings, none of which had any obvious
relation to any kind of string libraries.
I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourceforge.net
which is the same thing.
[...] Perhaps you would be so kind as
to provide a link, so that we can see whether your library meets the OP's
requirement of ANSI C.
Well it does, and I've provided the link. Perhaps you should call
customer support at your ISP.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #9

P: n/a
we******@gmail.com said:
Richard Heathfield wrote:
>we******@gmail.com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.

Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.
Um, so what? The existence of people who cannot understand C does not imply
the non-existence of people who can.

<snip>
>It's possible using the ordinary C string library too.

We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.
It could just be evidence of DYOH.

<snip>
I have written a
string library called "The Better String Library" which is perfectly
suited for this.

Of the two URLs in your sig, one failed to load at all, and the other
presented a bizarre set of cartoon drawings, none of which had any
obvious relation to any kind of string libraries.

I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourceforge.net
which is the same thing.
That worked. The compilation didn't, though. You're a bit cavalier with
constness.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #10

P: n/a
Richard Heathfield wrote:
we******@gmail.com said:
.... snip ...
>>
I am not in charge of the www or your access to it. I can get
the bstring.sf.net without difficulty. You can try
bstring.sourceforge.net which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier
with constness.
websnarl has also been known to assume 32 bit integers and 8 bit
chars.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Sep 26 '06 #11

P: n/a
CBFalconer said:
Richard Heathfield wrote:
>we******@gmail.com said:
... snip ...
>>>
I am not in charge of the www or your access to it. I can get
the bstring.sf.net without difficulty. You can try
bstring.sourceforge.net which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier
with constness.

websnarl has also been known to assume 32 bit integers and 8 bit
chars.
That's a tempting assumption to make at times. But I can find no definitive
evidence of either assumption in bstrlib (after a very cursory inspection).

I would consider it reasonable to make the 8-bit char assumption provided
that the code checks that the assumption is true and fails to compile if it
is false (#if CHAR_BIT 8 / #error ...), and provided that the reasons for
the assumption are made clear, and provided that guidance is given on what
areas would need looking at if someone were to attempt to remove the
dependence on that assumption.

For example, there are times when you'd sure like to have an array with one
element for each possible unsigned char value. (Boyer-Moore springs to
mind.) If CHAR_BIT is 8, this involves a mere 256 * sizeof(type) bytes. If
CHAR_BIT is 16, however, the overhead becomes significant. And if it's 32,
well... even if it's just an array of unsigned char, you're still talking
16 Gigoctets, which is very significant indeed!

I can see no plausible necessity for assuming that integers have 32 bits,
however. But I stress again that I have seen no definitive evidence that Mr
Websnarf has assumed this. Despite the plethora of 32s in the code, there
are significant indications that he is aware of the fact that integers need
not have 32 bits.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #12

P: n/a
Richard Heathfield wrote:
we******@gmail.com said:
Richard Heathfield wrote:
we******@gmail.com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.
Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not imply
the non-existence of people who can.
But you have not provided any evidence of people who can, outside an
assertion. Where's your solution to the OP's problem? Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.
It's possible using the ordinary C string library too.
We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>
Every question on c.l.c could be a DYOH question. But this one is easy
to recognize as not being a homework question. Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C -- but at the same time there are
really very few fundamental *concepts* being taught in an exercise like
this. The only value of this exercise, to solve in C, is to learn what
a really horridly useless language C is when dealing with strings and
ADTs in general, which most institutions would not bother spending an
exercise on.

Its interesting how you see things so narrowly: there *could* be
programmers that can use the standard libraries string functions
perfectly, therefore that *must* be the right way to do things. Any
question *could* be a homework question, therefore it *is* a homework
question. You know the battery in your computer? It *could* explode;
you better stop using it.
I have written a
string library called "The Better String Library" which is perfectly
suited for this.

Of the two URLs in your sig, one failed to load at all, and the other
presented a bizarre set of cartoon drawings, none of which had any
obvious relation to any kind of string libraries.
I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourceforge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.
Well I didn't check it, but I highly doubt that "const" is my problem.
Looking at it again, it could be that some compiler is annoyed by my
coercion of void * to bstring. It should be a warning at most though.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #13

P: n/a
we******@gmail.com said:
Richard Heathfield wrote:
>we******@gmail.com said:
Richard Heathfield wrote:
we******@gmail.com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage,
though.

Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not
imply the non-existence of people who can.

But you have not provided any evidence of people who can, outside an
assertion.
Is it your claim that no such people exist? I offer as obvious
counter-examples the following (non-exhaustive!) list: Dennis Ritchie,
Brian Kernighan, Doug Gwyn, Nick Maclaren, Chris Torek, Lawrence Kirby, Dan
Pop, Chris Dollin, Ben Pfaff, Dann Corbit, Peter Seebach, Clive Feather,
Greg Comeau, and P J Plauger. None of these would have the slightest
problem sorting an array of strings.
Where's your solution to the OP's problem?
I chose not to write one. I am under no obligation to write one. Yes, I
could have provided an example that used my own string library, much as you
did with yours. But that would, IMHO, not have been in keeping with the
OP's request for an ANSI C solution, which suggested to me that he didn't
want a canned response involving a large library. (I accept that I could be
mistaken about that.)

Or I could have coded one up from scratch. I doubt whether there are many
people here (other than, perhaps, yourself) who would question my ability
to do that. It is, after all, a simple enough exercise. Populate the array,
and call qsort. Easy.
Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.
If it's such drivel, why respond to it?
>It's possible using the ordinary C string library too.

We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>

Every question on c.l.c could be a DYOH question.
Indeed.
But this one is easy to recognize as not being a homework question.
I think not.
Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C
....but Bstrlib is written in C! Sheesh, get a life.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #14

P: n/a
Richard Heathfield schrieb:
we******@gmail.com said:
>>pkirk25 wrote:
<snip>
>>>I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

So you want to sort the lines of text alphabetically right?
>>>I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.

Close. It's a sequence of adjacent characters (typically stored in an array,
yes) that is terminated by the first null character in that sequence.
>>In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.
I _can_ do many things, including reinventing the (polygonal) wheel time
and again. (ISO) C, in my opinion, is not the language to use if you
heavily operate on / with strings and want results quickly.
<snip>
>>By using more advanced strings (i.e., not the crap that the language
comes with),

Not everyone shares websnarf's opinion.
>>this is possible in the C language.

It's possible using the ordinary C string library too.
Yes, it is.
As I wrote in my response to the OP, <4n************@individual.net>,
C IMO is not the "best" or "easiest" language for this task.
As long as the OP does not suffer through this for learning purposes,
I see no point in restricting oneself to standard C.
I do not care at all for websnarf's acerbic to poisonous tone and
unobjective colouring of his assessments, though.
>>I have written a
string library called "The Better String Library" which is perfectly
suited for this.
<snipped by Richard: Code using bstrlib>

The code looks sufficiently clear, I'd still rather use Perl or python.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 26 '06 #15

P: n/a
Richard Heathfield wrote:
we******@gmail.com said:
Richard Heathfield wrote:
we******@gmail.com said:
Richard Heathfield wrote:
we******@gmail.com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage,
though.

Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not
imply the non-existence of people who can.
But you have not provided any evidence of people who can, outside an
assertion.

Is it your claim that no such people exist?
No, I claim that you have not provided any evidence. Think of it from
the OP's point of view. Suppose he has a question in his mind about
whether or not its even possible to solve the problem in C. How would
you convince him that it was possible? (Well you could always point
him to my post ...)
[...] I offer as obvious
counter-examples the following (non-exhaustive!) list: Dennis Ritchie,
Brian Kernighan, Doug Gwyn, Nick Maclaren, Chris Torek, Lawrence Kirby, Dan
Pop, Chris Dollin, Ben Pfaff, Dann Corbit, Peter Seebach, Clive Feather,
Greg Comeau, and P J Plauger. None of these would have the slightest
problem sorting an array of strings.
Uh ... probably so; I'd even be willing to bet CBFalconer could do it,
but its interesting that you should go to such an "illustrious list"
for such ridiculously simple problem -- and yet at the same time, still
provide no solution to the problem at hand.

(BTW, I think you have to remove Peter Seebach from that list. I've
seen some of his more marvellous efforts -- programming is not his
strong suit. Assuming you've fixed your internet go check this out:
http://www.seebs.net/ops/ibm/email/sz.c and the sz.h file. Its like
the MS3K of C programming; its so bad its good.)
Where's your solution to the OP's problem?

I chose not to write one. I am under no obligation to write one. Yes, I
could have provided an example that used my own string library, much as you
did with yours. But that would, IMHO, not have been in keeping with the
OP's request for an ANSI C solution,
Uhh... just trying to untangle that logic -- you are saying you have
your own C library for strings that you use that isn't ANSI compliant?
[...] which suggested to me that he didn't
want a canned response involving a large library.
Well that's interesting -- I just don't follow your reasoning at all.
Is there something in the ANSI C language standard that says large
libraries are disallowed?
(I accept that I could be mistaken about that.)
Yathink?
Or I could have coded one up from scratch. I doubt whether there are many
people here (other than, perhaps, yourself) who would question my ability
to do that.
I question the effort required.
[...] It is, after all, a simple enough exercise. Populate the array,
and call qsort. Easy.
Oh yeah, real easy. So easy, you're not even going to do it.
Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.

If it's such drivel, why respond to it?
Because it required no effort? (You're not following along are you?)
It's possible using the ordinary C string library too.

We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>
Every question on c.l.c could be a DYOH question.

Indeed.
But this one is easy to recognize as not being a homework question.

I think not.
I'm sure Warcraft is really important subject matter for that class
he's taking.
Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C

...but Bstrlib is written in C! Sheesh, get a life.
So did you ever take a course in formal logic? I mean seriously,
what's your problem? You know Bstrlib has been written over a period
of several years and I've received lots of feedback on improving it (so
technically some changes and ideas were technically beyond my
thinking/experience). So if I could argue, for a second, that Bstrlib
was a pain in the ass to write, then what exactly is your problem?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #16

P: n/a
Richard Heathfield <in*****@invalid.invalidwrites:
we******@gmail.com said:
[...]
>I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourceforge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.
Really? It compiled for me with no diagnostics.

I used:

gcc -ansi -pedantic -W -Wall -O3 -c bstrlib.c

using gcc versions 2.7.2.2, 3.4.4, and 4.0.2.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 26 '06 #17

P: n/a
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalidwrites:
>we******@gmail.com said:
[...]
>>I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourceforge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.

Really?
Yes.
It compiled for me with no diagnostics.

I used:

gcc -ansi -pedantic -W -Wall -O3 -c bstrlib.c

using gcc versions 2.7.2.2, 3.4.4, and 4.0.2.
FYI, I used -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 with gcc 2.95.3.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #18

P: n/a
Its always interesting to come back to a newsgroup and see a thread
that has taken on a life of its own.

Fond though I am of World of Warcraft, my project is mainly about
learning C.

Why C instead of C# or Java?

Once upon a time i was a programmer in VB6. I will never touch any
language that is the plaything of a vendor. I still use MSAccess VBA
for projects and find myself wondering why MS decided to kill VB for
those of us who regard OOP as a distraction.

I know from hiring C++ developers that it takes 2 years full time work
experience in C++ to become productive. Even then, C++ developers seem
to have semi-religous practices that require them to abuse other C++
developers. Worse, when one leaves, the "better" his coding standards,
the less likely you are to hire someone who can understand his
object-orientated aspect-guided paradigm-style code and you have to pay
for the whole lot to be done again! So for my hobby project to
automating World of Warcraft gold trading, C++ may well be a better
tool but I don't have 2 full years to devote to any hobby.

C is small - you can learn it it a few weeks - I started last Thursday
and already have something that is usable. Almost every evening, when
I start to code, I watch the damn thing crash. But that feeling of
being able to make it work and my it work ANY which way you choose is
why I chose C. If I wanted to do things the "right" way, I'd be a VB
..NET junkie waiting for MS to decide that VB. NET is a dead end.

I post this in the hope that those who have become very heated about
how best to help me will feel a little better about my bumbling efforts
at reinventing the wheel again and again.

Patrick

Sep 26 '06 #19

P: n/a
"pkirk25" <pa*****@kirks.netwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
Two great answers.

Thanks - if I can make it all fit together I may well become the first
World of Warcraft gold millionaire!
Add:

And p.s. You will get nothing for writing it for me!

--
Mabden
Oct 1 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.