Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #define MSG_LENGTH 64
- int find_length(char* line) {
- int i = 0;
- for (;;) {
- if (line[i] == 0) break;
- i++;
- }
- return i;
- }
- void rotate_line(char* in, char* out) {
- int i, j, l;
- l = find_length(in);
- if (l == 0) return;
- i = l - 1;
- j = 0;
- for (;;) {
- out[j] = in[i];
- j++;
- if (i == 0) break;
- i--;
- }
- }
- void fill_line(char* in) {
- int i;
- i = find_length(in);
- for (;;) {
- if (i == MSG_LENGTH) break;
- in[i] = 32 + rand()%95;
- i++;
- }
- }
- void copy_line(char* in, char* out) {
- int i;
- i = 0;
- for (;;) {
- if (i == MSG_LENGTH) break;
- out[i] = in[i];
- i++;
- }
- }
- void shift_left(char* in, char* out, int shft) {
- int i, j;
- j = 0;
- i = shft;
- for (;;) {
- if (i == MSG_LENGTH) break;
- out[j] = in[i];
- i++;
- j++;
- }
- i = 0;
- for (;;) {
- if (i == shft) break;
- out[j] = in[i];
- i++;
- j++;
- }
- }
- void shift_right(char* in, char* out, int shft) {
- int i, j;
- j = shft;
- i = 0;
- for (;;) {
- if (i == MSG_LENGTH-shft) break;
- out[j] = in[i];
- j++;
- i++;
- }
- j = 0;
- for (;;) {
- if (i == MSG_LENGTH) break;
- out[j] = in[i];
- j++;
- i++;
- }
- }
- void clear_line(char* in) {
- int i;
- i = 0;
- for (;;) {
- if (i == MSG_LENGTH+1) break;
- in[i] = 0;
- i++;
- }
- }
- void erase_enters(char* in) {
- int i;
- i = 0;
- for (;;) {
- if (i == MSG_LENGTH+1) break;
- if (in[i] == 0x0a) in[i]=0x0;
- i++;
- }
- }
- int sum_chars(char* in) {
- int i, s;
- i = 0;
- s = 0;
- for (;;) {
- if (i == MSG_LENGTH) break;
- s = s + in[i];
- i++;
- }
- return s;
- }
- int main(int argc, char *argv[]) {
- srand(time(NULL)); // should only be called once
- int i, j, rot, shft, direction, sum;
- char msg[MSG_LENGTH+1];
- char tmp[MSG_LENGTH+1];
- clear_line(msg);
- clear_line(tmp);
- printf("Enter a line:\n");
- fgets(msg, MSG_LENGTH+1, stdin);
- erase_enters(msg);
- printf("Got a line:\n%s\nLength = %i\n", msg, find_length(msg));
- /* Fill Line */
- fill_line(msg);
- /* Sum of Chars */
- sum = sum_chars(msg);
- printf("Sum of chars = %i\n", sum);
- /* ??? ROTATE ??? */
- if (argc > 1)
- rot = ( atoi(argv[1]) ) % 2;
- else
- rot = ( rand() + sum ) % 2;
- if (rot == 1) {
- rotate_line(msg, tmp);
- copy_line(tmp, msg);
- }
- printf("Rotate = %i\n", rot);
- /* Print... */
- printf("Filled (and rotated?) line:\n%s\n", msg);
- printf("Last chars: 0x%08x, 0x%08x, 0x%08x\n",
- msg[MSG_LENGTH-2], msg[MSG_LENGTH-1], msg[MSG_LENGTH]);
- /* ??? SHIFT ??? */
- if (argc > 2)
- shft = ( atoi(argv[2]) ) % MSG_LENGTH;
- else
- shft = ( rand() + sum ) % MSG_LENGTH;
- printf("Shift = %i\n", shft);
- /* ??? DIRECTION ??? */
- if (argc > 3)
- direction = ( atoi(argv[3]) ) % 2;
- else
- direction = ( rand() + sum ) % 2;
- printf("Direction = %i\n", direction);
- /* SHIFTING... */
- if (direction == 0)
- shift_left(msg, tmp, shft);
- else
- shift_right(msg, tmp, shft);
- copy_line(tmp, msg);
- printf("Shifted line:\n%s\n", msg);
- /* ... */
- printf("Last chars: 0x%08x, 0x%08x, 0x%08x\n",
- msg[MSG_LENGTH-2], msg[MSG_LENGTH-1], msg[MSG_LENGTH]);
- return 0;
- }
Add Comment
Please, Sign In to add comment