daily pastebin goal
69%
SHARE
TWEET

C algorithms for Kolibri

a guest Jun 29th, 2018 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. #define MSG_LENGTH 64
  6.  
  7. int find_length(char* line) {
  8.     int i = 0;
  9.     for (;;) {
  10.         if (line[i] == 0) break;
  11.         i++;
  12.     }
  13.     return i;
  14. }
  15.  
  16. void rotate_line(char* in, char* out) {
  17.     int i, j, l;
  18.     l = find_length(in);
  19.     if (l == 0) return;
  20.     i = l - 1;
  21.     j = 0;
  22.     for (;;) {
  23.         out[j] = in[i];
  24.         j++;
  25.         if (i == 0) break;
  26.         i--;
  27.     }
  28. }
  29.  
  30. void fill_line(char* in) {
  31.     int i;
  32.     i = find_length(in);
  33.     for (;;) {
  34.         if (i == MSG_LENGTH) break;
  35.         in[i] = 32 + rand()%95;
  36.         i++;
  37.     }
  38. }
  39.  
  40. void copy_line(char* in, char* out) {
  41.     int i;
  42.     i = 0;
  43.     for (;;) {
  44.         if (i == MSG_LENGTH) break;
  45.         out[i] = in[i];
  46.         i++;
  47.     }
  48. }
  49.  
  50. void shift_left(char* in, char* out, int shft) {
  51.     int i, j;
  52.     j = 0;
  53.     i = shft;
  54.     for (;;) {
  55.        if (i == MSG_LENGTH) break;
  56.        out[j] = in[i];
  57.        i++;
  58.        j++;
  59.     }
  60.     i = 0;
  61.     for (;;) {
  62.        if (i == shft) break;
  63.        out[j] = in[i];
  64.        i++;
  65.        j++;
  66.     }
  67. }
  68.  
  69. void shift_right(char* in, char* out, int shft) {
  70.     int i, j;
  71.     j = shft;
  72.     i = 0;
  73.     for (;;) {
  74.        if (i == MSG_LENGTH-shft) break;
  75.        out[j] = in[i];
  76.        j++;
  77.        i++;
  78.     }
  79.     j = 0;
  80.     for (;;) {
  81.        if (i == MSG_LENGTH) break;
  82.        out[j] = in[i];
  83.        j++;
  84.        i++;
  85.     }
  86. }
  87.  
  88. void clear_line(char* in) {
  89.     int i;
  90.     i = 0;
  91.     for (;;) {
  92.         if (i == MSG_LENGTH+1) break;
  93.         in[i] = 0;
  94.         i++;
  95.     }
  96. }
  97.  
  98. void erase_enters(char* in) {
  99.     int i;
  100.     i = 0;
  101.     for (;;) {
  102.         if (i == MSG_LENGTH+1) break;
  103.         if (in[i] == 0x0a) in[i]=0x0;
  104.         i++;
  105.     }
  106. }
  107.  
  108. int sum_chars(char* in) {
  109.     int i, s;
  110.     i = 0;
  111.     s = 0;
  112.     for (;;) {
  113.         if (i == MSG_LENGTH) break;
  114.         s = s + in[i];
  115.         i++;
  116.     }
  117.     return s;
  118. }
  119.  
  120. int main(int argc, char *argv[]) {
  121.  
  122.     srand(time(NULL));   // should only be called once
  123.  
  124.     int i, j, rot, shft, direction, sum;
  125.  
  126.     char msg[MSG_LENGTH+1];
  127.     char tmp[MSG_LENGTH+1];
  128.  
  129.     clear_line(msg);
  130.     clear_line(tmp);
  131.  
  132.     printf("Enter a line:\n");
  133.     fgets(msg, MSG_LENGTH+1, stdin);
  134.     erase_enters(msg);
  135.     printf("Got a line:\n%s\nLength = %i\n", msg, find_length(msg));
  136.  
  137.     /* Fill Line */
  138.  
  139.     fill_line(msg);
  140.  
  141.     /* Sum of Chars */
  142.  
  143.     sum = sum_chars(msg);
  144.     printf("Sum of chars = %i\n", sum);
  145.  
  146.     /* ??? ROTATE ??? */
  147.  
  148.     if (argc > 1)
  149.         rot = ( atoi(argv[1]) ) % 2;
  150.     else
  151.         rot = ( rand() + sum ) % 2;
  152.  
  153.     if (rot == 1) {
  154.         rotate_line(msg, tmp);
  155.         copy_line(tmp, msg);
  156.     }
  157.  
  158.     printf("Rotate = %i\n", rot);
  159.  
  160.     /* Print... */
  161.  
  162.     printf("Filled (and rotated?) line:\n%s\n", msg);
  163.     printf("Last chars: 0x%08x, 0x%08x, 0x%08x\n",
  164.             msg[MSG_LENGTH-2], msg[MSG_LENGTH-1], msg[MSG_LENGTH]);
  165.  
  166.     /* ??? SHIFT ??? */
  167.  
  168.     if (argc > 2)
  169.         shft = ( atoi(argv[2]) ) % MSG_LENGTH;
  170.     else
  171.         shft = ( rand() + sum ) % MSG_LENGTH;
  172.  
  173.     printf("Shift = %i\n", shft);
  174.  
  175.     /* ??? DIRECTION ??? */
  176.  
  177.     if (argc > 3)
  178.         direction = ( atoi(argv[3]) ) % 2;
  179.     else
  180.         direction = ( rand() + sum ) % 2;
  181.  
  182.     printf("Direction = %i\n", direction);
  183.  
  184.     /* SHIFTING... */
  185.  
  186.     if (direction == 0)
  187.         shift_left(msg, tmp, shft);
  188.     else
  189.         shift_right(msg, tmp, shft);
  190.  
  191.     copy_line(tmp, msg);
  192.  
  193.     printf("Shifted line:\n%s\n", msg);
  194.  
  195.     /* ... */
  196.  
  197.     printf("Last chars: 0x%08x, 0x%08x, 0x%08x\n",
  198.             msg[MSG_LENGTH-2], msg[MSG_LENGTH-1], msg[MSG_LENGTH]);
  199.  
  200.     return 0;
  201. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top