Mail Archives: djgpp/2001/03/19/20:01:46
jaystewart1 wrote:
>
> Can you guys help me write a program that outputs two strings and their
> length, then outputs the two strings in alphabetical order?
Show us (better: show comp.lang.c or alt.comp.lang.learn.c-c++) your
attempt and we will try to help fix your code.
>
> The array below represents the ASCII codes for a string. Can anyone help me
> write a program that outputs the corresponding string?
>
> int x[] ={65,112,114,105,108,32,115,104,111,119,
> 101,114,115,32,98,114,105,110,103,32,77,97,
> 121,32,102,108,111,119,101,114,115,32,33,0};
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x[] =
{ 65, 112, 114, 105, 108, 32, 115, 104, 111, 119, 101, 114, 115,
32, 98, 114, 105, 110, 103, 32, 77, 97, 121, 32, 102, 108,
111, 119, 101, 114, 115, 32, 33, 0
};
char *y;
size_t i;
if (!(y = malloc(sizeof x / sizeof *x))) {
printf("Damn!\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < sizeof x / sizeof *x; i++)
y[i] = x[i];
printf
("The 'correctness' of the following output depends\n"
"on the implementation using ASCII, by no means\n"
"guaranteed by the standard. Here we go:\n %s\n\n", y);
printf
("Note that 'y' in the above statement could not be replaced by\n"
"(char *)x because x[0] interpreted as chars is \\%03o \\%03o\n",
(char)*(char *)&x, (char)(*(1 + (char *)&x)));
free(y);
return 0;
}
The 'correctness' of the following output depends
on the implementation using ASCII, by no means
guaranteed by the standard. Here we go:
April showers bring May flowers !
Note that 'y' in the above statement could not be replaced by
(char *)x because x[0] interpreted as chars is \101 \000
- Raw text -