Skip to content

Instantly share code, notes, and snippets.

@w495
Created January 16, 2019 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w495/0ba11028509ffeaa9fe3f911d43db2d9 to your computer and use it in GitHub Desktop.
Save w495/0ba11028509ffeaa9fe3f911d43db2d9 to your computer and use it in GitHub Desktop.
Non-trivial reverse with offset and limit
#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;
}
@w495
Copy link
Author

w495 commented Jan 16, 2019

with

  • offset = 1;
  • limit = 4;

input (13)

    0 => a
    1 => b
    2 => c
    3 => d
    4 => e
    5 => f
    6 => g
    7 => i
    8 => j
    9 => k
    10 => l
    11 => m
    12 => n

wrong reverse out (4)

     0 => e
     1 => d
     2 => c
     3 => b

reverse out (4)

     0 => m
     1 => l
     2 => k
     3 => j

direct out (4)

     0 => b
     1 => c
     2 => d
     3 => e

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment