Re: How to print an array of char backward.



In comp.lang.c, sohijb.edrisy@xxxxxxxxx wrote:

You could reverse the string in place before printing it. There is no
standard string reverse function, but it's easy enough to write one. As
an alternative you could just iterate backwards through the string
while printing it, character by character.

<snip>

Here's what I should do.

(Put this after the do-while{} loop)

////////////////////////////////////////////////////////////////////////
length = strlen(bin);
int i;

for (i = length - 1; i >= 0; i--)
{
printf("%c", bin[i]);
}
///////////////////////////////////////////////////////////////////////

How about...

void RevPrint(char *string)
{
if (*string)
{
RevPrint(string+1);
printf("%c",*string);
}
}

used as...
printf("Binary representation: "); RevPrint(bin);



--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)