> mystery(char *s, char *t) {
> while(*s++ = *t++)
> ;
> }
On Fri, 14 Oct 2011, Josh Dersch wrote:
And I would tend to prefer a loop that involves some
sort of bounds
checking :).
It DOES. As much as C "normally" does. (Bounds checking is not often
considered important in C! It is EASY to crash almost any program that
uses scanf(), or even gets() for keyboard input! )
It will continue until the value that has been copied is 0
It IS the code, that you've probably been using all along, of
strcpy() . And THAT is the level of bounds checking that the "standard
library function" has.
BUT, I would prefer swapping 's' and 't' throughout the example, to make
it easier for those not familiar with it to be able to think of them as
"source" and "target".
When I taught "Data Structures and Algorithms In C", meaningful variable
names DID help students to follow what was going on.
After we finished that (building a string copy from array notation spread
out to THAT, and a trivial strcmp() ), we spent the rest of that lecture
on "alphabetic order".
Yeah, you know alphabetic order. Yeah. SURE.
Q: But can you recite it backwards, WITHOUT SINGING, standing on one leg?
A: Not while sober.
Which order do YOU think the following belong in? (and why?) (and what
order matches a sort based on strcmp() ? (and why?) ) :
New York
new compiler
newsworthy
two
three (a hint)
new world (deliberate white space, as a hint for New York/Newton)
Int21h
Newark
C : An Introduction (proper bibliographic punctuation of subtitle)
C: An Introduction
Int3h
New Hampshire
Newton
800 Software (and WHERE in the phonebook was it (it was a real company))
99 Ranch Market (and WHERE in the phonebook (it is a real chain)) (The
college discontinued "advanced" programming classes while phone
books still existed)
Then they had an assignment (due in 2 weeks, but with discussion next
week) to write their own strcmp() that could, under program control:
ignore/pay attention to case
ignore/pay attention to NUMERALS
put numbers in numeric order, even if not at beginning/end of item
place numerals in ASCII/numerical/phone book order
ignore/pay attention to space ("order by letter"/"order by word" to
OLDER
librarians)
ignore/pay attention to other "white space" and any other arbitrary characters
Since strcmp()'s return value is "normally" only checked for 0 / <0 /
>0 ,
I introduced them to the concept that it COULD, instead of a simple
subtraction, return packed bitfield that would could be used by the
calling routine to mimic strcmp(), but by masking bits (an earlier
lecture) could identify if something was a match except for case, etc.
The next lecture started in on sorting and searching algorithms.
--
Grumpy Ol' Fred cisin at
xenosoft.com