gpt4 book ai didi

c - 实现换行的不同方式

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:08 25 4
gpt4 key购买 nike

我正在尝试使用行长来换行并一次打印一行(行长),而不是将整行打印为一个 block 。我认为在每一行长度处添加 '\n' 字符就可以完成工作并编写了以下代码。

void wrap(char *tok, int len)
{
int i, h;
char *ss = tok;
for (h = linelen; h < (len + 1); (h + linelen))
{
ss[h] = '\n';
printf("%d\n", h);
h += linelen;
}
char *token = strtok(ss, "\n");
while (token)
{
printf("%s\n", token);
token = strtok(NULL, "\n");
}
}

但是这段代码似乎不起作用。还有哪些方法可以实现换行?

最佳答案

从这篇文章中被盗:Is it possible to print out only a certain section of a C-string, without making a separate substring?

#include <stdio.h>
#include <string.h>

int main(void) {

char *str = "helo world";
int total_len = strlen(str);
int single_len = 3;
for (int i = 0; i < total_len; i += single_len) {
/* print at most first three characters (safe) */
printf("%.*s\n", single_len, str + i);
}

return 0;
}

关于c - 实现换行的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com