center_string(str, width)
len = get length of string
if len > width then
return str
num_left = (width-len)/2
num_right = width-len-num_left
return " " * num_left + str + " " * num_right
end
char *center_string(char *arg, int maxlen)
{
static char buf[MAX_STRING_LEN];
int arglen = UMIN(strlen(arg), maxlen);
memset(buf, ' ', maxlen);
memcpy(&buf[(maxlen - arglen) / 2], arg, arglen);
buf[maxlen] = 0;
return buf;
}
const char* center_string(const char* str, size_t width)
{
static char result[MAX_STRING_LENGTH];
size_t len = strlen(str);
if (len >= width) {
return str;
}
size_t num_left = (width-len)/2;
// Left-side spaces:
for (size_t i = 0; i < num_left; i++) {
result[i] = ' ';
}
// Copy the string:
for (size_t i = num_left; i < len + num_left; i++) {
result[i] = str[i-num_left];
}
// Finally, fill in the right spaces:
for (size_t i = len + num_left; i < width; i++) {
result[i] = ' ';
}
// Terminate the string
result[width] = '\0';
return result;
}
$ cat center.c
#include <string.h>
#include <stdio.h>
center(const char * s,int w){
int sl = strlen(s), l = (w-sl)/2, r = (w-sl)-l;
printf("[%*s%s%*s]\n",l,"",s,r,"");}
int main() {
center("CLANS",15);
center("===============",15);
center("Whoop-Ass",15);
center("Huns",15);
center("Tiny Fairy Queens",15); // ugly
}
$ gcc center.c & ./a
[ CLANS ]
[===============]
[ Whoop-Ass ]
[ Huns ]
[ Tiny Fairy Queens ]
int colorstrlen(char *argument)
{
char *str;
int strlength;
if (argument == NULL || argument[0] == '\0')
return 0;
strlength = 0;
str = argument;
while (*str != '\0')
{
if ( *str != '{' )
{
str++;
strlength++;
continue;
}
if (*(++str) == '{')
strlength++;
str++;
}
return strlength;
}
$ cat center.c
#include <string.h>
#include <stdio.h>
center(const char * s,int w){
int sl = strlen(s), tl = (w-sl)<0?0:(w-sl) , l = tl/2, r = tl-l;
printf("[%*s%.*s%*s]\n",l,"",w,s,r,"");
}
int main() {
center("CLANS",15);
center("===============",15);
center("Whoop-Ass Clan",15);
center("Huns",15);
center("Tiny Fairy Queens",15);
center("A really really long clan name",15);
center("A",15);
}
$ gcc center.c & ./a
[ CLANS ]
[===============]
[Whoop-Ass Clan ]
[ Huns ]
[Tiny Fairy Quee]
[A really really]
[ A ]
int colorstrlen(char *argument)
{
char *str;
int strlength;
if (argument == NULL || argument[0] == '\0')
return 0;
strlength = 0;
str = argument;
while (*str != '\0')
{
if ( *str != '{' )
{
str++;
strlength++;
continue;
}
if (*(++str) == '{')
strlength++;
str++;
}
return strlength;
}
char buf[MSL];
char temp[MSL];
int sn, i = 0, x = 0;
int sl = 0;
if (IS_NPC(ch))
return;
for (sn = 0; sn < (1 + top_clan); sn++)
{
if (clan_table[sn].name != '\0'
|| clan_table[sn].name != NULL)
{
i = colorstrlen(clan_table[sn].name);
}
// Check which name is bigger
if (x < i)
{
x = i;
sprintf (temp, "%s strlength %d\n\r", clan_table[sn].name, x);
sendch (temp, ch);
}
}
int l;
int r;
for (sn = 0; sn < (1 + top_clan); sn++)
{
if (clan_table[sn].name != '\0'
|| clan_table[sn].name != NULL)
{
sl = colorstrlen (clan_table[sn].name);
l = (x-sl)/2;
r = (x-sl)-L;
sprintf (buf, "{B[{c%*s%s%*s{B]{x", l, "",
clan_table[sn].name, r, "");
sendch (buf, ch);
}
}
i.e.:
so as you can see, they are all even in character length and spaced more to the right than left if an odd number of spaces is needed. I can easily do this editing guild.dat with each new clan but I use an OLC cedit code that I want this to be doable with, and my client deletes extra spaces so they always end up left aligned. (meaning future admins clients could do the same). Since the []' brackets are automatically added, I could simply remove that and that would allow for the extra spaces after the ' brackets are automatically added, I could simply remove that and that would allow for the extra spaces after the [ in doing 'cedit_who' to fix the problem but are there any simply codes to check for string length and add spaces before and after the string as necesarry to conform to a limit? Sorry for the long post about a potentially small problem, but I'm curious and wanted to provide as much information as possible for the answer I'm seeking.