Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: To reverse the digits of a numeral |
| Date | 2015-07-14 02:09 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87twt7mtot.fsf@bsb.me.uk> (permalink) |
| References | <digits-20150714024045@ram.dialup.fu-berlin.de> |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> To me, it makes non sense to "reverse the digits of a numeral".
>
> When you have stored a numeral, you can read it in any order you
> like. It has no direction! Therefore, it can't be "reversed".
I don't understand this. In this usage of the word numeral it's a
sequence of digits (also sometimes confusingly called numerals). A
sequence has an order and, if it's finite, can be reversed.
> #include <stdio.h> /* putchar */
> #include <string.h> /* strlen */
> #include <stddef.h> /* size_t */
>
> int main( void )
> { char const * const numeral = "123";
> size_t const length = strlen( numeral );
> char const * const numeral_top = numeral + length;
>
> for( char const * p = numeral; p < numeral_top; ++p )
> putchar( *p ); putchar( '\n' );
>
> for( char const * p = numeral_top - 1; p >= numeral; --p )
> putchar( *p ); putchar( '\n' ); }
That's undefined. I'd probably write
const char *p = numeral_top;
while (p > numeral)
putchar(*--p);
but there are obviously loads of alternatives.
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next | Find similar | Unroll thread
Re: To reverse the digits of a numeral Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-14 02:09 +0100
csiph-web