Created
January 16, 2019 18:34
-
-
Save w495/0ba11028509ffeaa9fe3f911d43db2d9 to your computer and use it in GitHub Desktop.
Non-trivial reverse with offset and limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
typedef unsigned char byte_t; | |
static const byte_t offset = 1; | |
static const byte_t limit = 4; | |
static const char in_list[] = { | |
'a', 'b', 'c', | |
'd', 'e', 'f', | |
'g', 'i', 'j', | |
'k', 'l', 'm', 'n' | |
}; | |
int main () { | |
static const byte_t len = sizeof(in_list); | |
char* out_list = malloc(sizeof(byte_t) * len); | |
byte_t i = 0; | |
printf("\noffset = %d;\n\nlimit = %d;\n\n # input (%d)\n\n", offset, limit, len); | |
for(i = 0; i != len; ++i) | |
printf("\t%d => %c\n", i, in_list[i]); | |
/* | |
* wrong reverse output | |
*/ | |
memset(out_list,0,len); | |
for (i = offset + limit; i > offset; --i) | |
out_list[offset + limit - i] = in_list[i - 1]; | |
printf("\n # wrong reverse out (%d) \n\n", limit); | |
for(i = 0; i != limit; ++i) | |
printf("\t %d => %c\n", i, out_list[i]); | |
/* | |
* write reverse output | |
*/ | |
memset(out_list,0,len); | |
for (i = offset; i < offset + limit; ++i) | |
out_list[i - offset] = in_list[len - i - 1]; | |
printf("\n # reverse out (%d) \n\n", limit); | |
for(i = 0; i != limit; ++i) | |
printf("\t %d => %c\n", i, out_list[i]); | |
/* | |
* write direct output | |
*/ | |
memset(out_list,0,len); | |
for (i = offset; i < offset + limit; ++i) | |
out_list[i - offset] = in_list[i]; | |
printf("\n # direct out (%d) \n\n", limit); | |
for(i = 0; i != limit; ++i) | |
printf("\t %d => %c\n", i, out_list[i]); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with
input (13)
wrong reverse out (4)
reverse out (4)
direct out (4)
`