shmuliar-sp-kr-lab4/src/swapper.c

49 lines
865 B
C
Raw Normal View History

2024-12-11 20:29:59 +02:00
#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;
}
}