add basic code

This commit is contained in:
ІО-23 Шмуляр Олег 2024-12-11 20:29:59 +02:00
commit b47037f66e
3 changed files with 74 additions and 0 deletions

9
inc/swapper.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __SWAPPER_HEADER
#define __SWAPPER_HEADER
#define __SWAPPER_MAX_STR_LENGTH 65535
int swapper_length_difference(char *, char *);
void swapper_swap(char *, char *);
#endif

17
src/main.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include "swapper.h"
int main(int argc, char **argv)
{
if (3 != argc)
return 1;
if (swapper_length_difference(argv[1], argv[2]))
return 2;
swapper_swap(argv[1], argv[2]);
printf("%s\n%s\n", argv[1], argv[2]);
return 0;
}

48
src/swapper.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include "swapper.h"
#define ABS(val) ( (val) >= 0 ? (val) : -(val) )
#define ALIGN8(val) ( (((val) >> 3) << 3) )
union tmp {
long int l;
char c;
};
static int strlen_c(char *a)
{
int len;
for (len = 0; len < __SWAPPER_MAX_STR_LENGTH; len++) {
if (a[len] == '\0')
break;
}
return len;
}
int swapper_length_difference(char *a, char *b)
{
int len_a = strlen_c(a);
int len_b = strlen_c(b);
return ABS(len_a - len_b);
}
void swapper_swap(char *a, char *b) {
int len = strlen_c(a);
union tmp t;
for (int i = 0; i < (len >> 3); i++) {
t.l = ((long int*) a)[i];
((long int*) a)[i] = ((long int*) b)[i];
((long int*) b)[i] = t.l;
}
for (int i = ALIGN8(len); i < len; i++) {
t.c = a[i];
a[i] = b[i];
b[i] = t.c;
}
}