Skip to content

Instantly share code, notes, and snippets.

@Rijen
Created May 15, 2024 09:17
Show Gist options
  • Save Rijen/253abf903701386dc255e95662035676 to your computer and use it in GitHub Desktop.
Save Rijen/253abf903701386dc255e95662035676 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <locale.h>
#include <string.h>
void center_string(char *res_buf, char *s, int len)
{
int l_offset, r_offset;
l_offset = r_offset = (len - len % 2) / 2;
int slen = strlen(s);
int str_right_half = (slen - slen % 2) / 2;
l_offset += str_right_half;
r_offset -= str_right_half;
char format_str[9];
sprintf(format_str, "%%%ds%%%ds", l_offset, r_offset);
sprintf(res_buf, format_str, s, " ");
}
void subst(char *s, char from, char to)
{
while (*s == from)
*s++ = to;
}
int main(void)
{
char *locale = setlocale(LC_ALL, "");
char buf[81];
sprintf(buf, "%080d", 0);
subst(buf, '0', '*');
printf("%s\n", buf);
char res_buf[81];
center_string(res_buf, "Leben ist schaisse!", 78);
printf("*\x1b[32m%s\x1b[0m*\n", res_buf);
printf("%s\n", buf);
center_string(res_buf, "Wir alle sterben!", 78);
printf("*\x1b[35m%s\x1b[0m*\n", res_buf);
printf("%s\n", buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment