用破折号格式化字符串的 C 函数

发布于 2024-10-12 20:37:57 字数 195 浏览 5 评论 0原文

任何人都可以通过将字符串 anand 填充长度作为输入来创建格式如下例所示的 ac 函数。字符串应该居中对齐,并且填充(破折号)的长度是恒定的(例如 45),并且字符串 len 不会超过填充长度。

例子#1 --------------关于------------

示例#1

----------我的付款-------- --

Can anyone please create a c function which formats like below example by taking string anand padding lenth as input. The string should be center aligned and length of padding (dash) is constant (say 45) and string len won't exceed pad length.

example #1
--------------ABOUT------------

example #1

----------MY PAYMENTS----------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听你说爱我 2024-10-19 20:37:57

剧透警告!

这是一种不同的方法。找出要粘贴的位置,然后将其粘贴进去。

char sBase[] = "---------------------------------------------";
char sInsert[] = "ABOUT";
int nStart = (strlen (sBase) - strlen (sInsert)) / 2;   // Centre of sInsert goes in centre of sBase
if (nStart >= 0)    // Make sure sInsert isn't bigger than sBase
{
    memcpy (&sBase [nStart], sInsert, strlen (sInsert));    // Stick it in
}

Spoiler alert!

Here's a different approach. Work out where to stick it, then stick it in.

char sBase[] = "---------------------------------------------";
char sInsert[] = "ABOUT";
int nStart = (strlen (sBase) - strlen (sInsert)) / 2;   // Centre of sInsert goes in centre of sBase
if (nStart >= 0)    // Make sure sInsert isn't bigger than sBase
{
    memcpy (&sBase [nStart], sInsert, strlen (sInsert));    // Stick it in
}
孤寂小茶 2024-10-19 20:37:57

问题是“帮助创建”,而不是“创建”,所以这里有一些伪代码给你:)

let numberOfDashes = TARGET_LENGTH - labelLength
let numberOfLeftDashes = numberOfDashes/2
let numberOfRightDashes = numberOfDashes - numberOfLeftDashes

let paddedString =
    repeat('-',numberOfLeftDashes)
  + labelString
  + repeat('-',numberOfRightDashes)

我将留给你用 C 重写它,应该不难 - 如果有任何问题,请发表评论。

The question was to "help create", not "create", so here's some pseudocode for you :)

let numberOfDashes = TARGET_LENGTH - labelLength
let numberOfLeftDashes = numberOfDashes/2
let numberOfRightDashes = numberOfDashes - numberOfLeftDashes

let paddedString =
    repeat('-',numberOfLeftDashes)
  + labelString
  + repeat('-',numberOfRightDashes)

I'll leave it to you to rewrite that in C, shouldn't be hard - drop a comment if there are any problems.

郁金香雨 2024-10-19 20:37:57

在这里,这可以处理所有情况,包括比填充范围更长的文本:

char* padCentered(char* out, const char* str, const int len, const char padchar)
{
    size_t lenstr = strlen(str);
    const char* in = str;
    if (lenstr < len) {
        memset(out, padchar, len);        
    } else if (lenstr > len) {
        in = (str + (lenstr/2)) - (len/2);
        lenstr = len;
    }
    strncpy(out + ((len/2) - (lenstr/2)), in, lenstr);
    out[len] = '\0';
    return out;
}

void dopaddedstr()
{
    char buf[1024];
    printf("%s\n", padCentered(buf, "0123456789", 6, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 7, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 8, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 10, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 11, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 12, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 80, '-'));
}

输出:

234567
2345678
12345678
0123456789
0123456789-
-0123456789-
-----------------------------------0123456789-----------------------------------

Here you go, this handles all cases, including longer text than what fits into padded range:

char* padCentered(char* out, const char* str, const int len, const char padchar)
{
    size_t lenstr = strlen(str);
    const char* in = str;
    if (lenstr < len) {
        memset(out, padchar, len);        
    } else if (lenstr > len) {
        in = (str + (lenstr/2)) - (len/2);
        lenstr = len;
    }
    strncpy(out + ((len/2) - (lenstr/2)), in, lenstr);
    out[len] = '\0';
    return out;
}

void dopaddedstr()
{
    char buf[1024];
    printf("%s\n", padCentered(buf, "0123456789", 6, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 7, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 8, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 10, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 11, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 12, '-'));
    printf("%s\n", padCentered(buf, "0123456789", 80, '-'));
}

outputs:

234567
2345678
12345678
0123456789
0123456789-
-0123456789-
-----------------------------------0123456789-----------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文