是否可以仅打印 C 字符串的特定部分,而不创建单独的子字符串?

发布于 2024-12-09 22:29:28 字数 157 浏览 0 评论 0原文

假设我有以下内容:

char *string = "Hello, how are you?";

是否可以仅打印该字符串的最后 5 个字节?仅前 5 个字节怎么样?是否有某种 printf 的变体可以实现这一点?

Say I have the following:

char *string = "Hello, how are you?";

Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this?

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

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

发布评论

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

评论(4

淡淡的优雅 2024-12-16 22:29:28

是否可以仅打印该字符串的最后 5 个字节?

是的,只需传递一个指向倒数第五个字符的指针即可。您可以通过 string + strlen(string) - 5 确定这一点。

仅前 5 个字节怎么样?

使用精度说明符:%.5s

#include <stdio.h>
#include <string.h>
char* string = "Hello, how are you?";

int main() {
  /* print  at most the first five characters (safe to use on short strings) */
  printf("(%.5s)\n", string);

  /* print last five characters (dangerous on short strings) */
  printf("(%s)\n", string + strlen(string) - 5);

  int n = 3;
  /* print at most first three characters (safe) */
  printf("(%.*s)\n", n, string);

  /* print last three characters (dangerous on short strings) */
  printf("(%s)\n", string + strlen(string) - n);
  return 0;
}

Is it possible to print out only the last 5 bytes of this string?

Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) - 5.

What about the first 5 bytes only?

Use a precision specifier: %.5s

#include <stdio.h>
#include <string.h>
char* string = "Hello, how are you?";

int main() {
  /* print  at most the first five characters (safe to use on short strings) */
  printf("(%.5s)\n", string);

  /* print last five characters (dangerous on short strings) */
  printf("(%s)\n", string + strlen(string) - 5);

  int n = 3;
  /* print at most first three characters (safe) */
  printf("(%.*s)\n", n, string);

  /* print last three characters (dangerous on short strings) */
  printf("(%s)\n", string + strlen(string) - n);
  return 0;
}
尐偏执 2024-12-16 22:29:28

是的,该字符串的最后五个字节可以通过以下方式完成:

printf ("%s\n", &(string[strlen (string) - 5]));

前五个字节可以通过以下方式完成:

printf ("%.5s\n", string);

您也可以将两者结合起来以获取字符串中的子字符串。 how 一词可以打印为:

printf ("%.3s\n", &(string[strlen (string) + 7]));

You do必须小心字符串足够长才能正常工作。打印单字符字符串的最后五个字符将导致未定义的行为,因为索引以 -4 结束。换句话说,在尝试此操作之前请检查字符串长度。

Yes, the last five bytes of that string can be done with:

printf ("%s\n", &(string[strlen (string) - 5]));

The first five can be done with:

printf ("%.5s\n", string);

You can combine the two to get substrings within the string as well. The word how can be printed with:

printf ("%.3s\n", &(string[strlen (string) + 7]));

You do have to be careful that the string is long enough for this to work. Printing the last five characters of a one-character string will cause undefined behaviour since the index ends up at -4. In other words, check the string length before attempting this.

享受孤独 2024-12-16 22:29:28

正如 Rob 的最佳答案,但我会将其放入 foo()

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

void printsl(const char* s, int i, int k) {  // print-slice s[i:k] of c-string as in python
    char buff[20];  // set a ridiculous lot of space in case string s is very, very long...
    if (k == -1)
    k = strlen(s);
    sprintf(buff, "%%.%ds", k-i);
    printf(buff, s+i);
}
// too complicated?
void printslb(const char* s, int i, int k) {  // print-slice s[i:k] of c-string as in python : more basic (?)
    if (k == -1)
    k = strlen(s);
    for (int j=i; j<k; ++j)
    putchar(s[j]);
}

int main() {

    // use k=-1 to indicate the end, whatever it is, but printf() is better there.

    const char* s = "abcde_ghijk_mno";
    printsl(s, 0, -1); printf("\n");  // same as: printf(s);
    printsl(s, 4, -1); printf("\n");  // same as: printf(s+4);
    printsl(s, 0, 4); printf("\n");   // useful case
    printsl(s, 4, 8); printf("\n");   // useful case

    printslb(s, 0, -1); printf("\n");
    printslb(s, 4, -1); printf("\n");
    printslb(s, 0, 4); printf("\n");
    printslb(s, 4, 8); printf("\n");

    return 0;
}

as in Rob's top answer, but i would put it into a foo()

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

void printsl(const char* s, int i, int k) {  // print-slice s[i:k] of c-string as in python
    char buff[20];  // set a ridiculous lot of space in case string s is very, very long...
    if (k == -1)
    k = strlen(s);
    sprintf(buff, "%%.%ds", k-i);
    printf(buff, s+i);
}
// too complicated?
void printslb(const char* s, int i, int k) {  // print-slice s[i:k] of c-string as in python : more basic (?)
    if (k == -1)
    k = strlen(s);
    for (int j=i; j<k; ++j)
    putchar(s[j]);
}

int main() {

    // use k=-1 to indicate the end, whatever it is, but printf() is better there.

    const char* s = "abcde_ghijk_mno";
    printsl(s, 0, -1); printf("\n");  // same as: printf(s);
    printsl(s, 4, -1); printf("\n");  // same as: printf(s+4);
    printsl(s, 0, 4); printf("\n");   // useful case
    printsl(s, 4, 8); printf("\n");   // useful case

    printslb(s, 0, -1); printf("\n");
    printslb(s, 4, -1); printf("\n");
    printslb(s, 0, 4); printf("\n");
    printslb(s, 4, 8); printf("\n");

    return 0;
}
玻璃人 2024-12-16 22:29:28

两种解决方案:

假设给定一个具有相同长度的可谓字符串 - 我将使用日期作为示例,并要求拆分为 HH:MM:SS.DDDDDDDD

char date[14] = "2359591234567";

[1] 可读实现:

char hh[3] = {0};
char mm[3] = {0};
char ss[3] = {0};
char dec[8] = {0};
strncpy ( hh, date, 2 );
strncpy ( mm, date+2, 2 );
strncpy ( ss, date+4, 2 );
strncpy ( dec, date+6, 7 );

printf("%s:%s:%s.%s\n", hh, mm, ss, dec);

[2] 简短实现:

要么:

printf("%.2s:%.2s:%.2s.%.7s\n", date, date+2, date+4, date+6);

要么:

printf("%2.2s:%2.2s:%2.2s.%7.7s\n", date, date+2, date+4, date+6);

应该有效。

您可以使用 sprintf 代替 printf 并复制到缓冲区。我还会检查正确的长度以避免不可预测的行为。

无论哪种情况 - 输出都将是:

23:59:59.1234567

Two solutions:

Say given a predicatable string with same length - I will use date as an example and asked to split into HH:MM:SS.DDDDDDD

char date[14] = "2359591234567";

[1] Readable Implementation:

char hh[3] = {0};
char mm[3] = {0};
char ss[3] = {0};
char dec[8] = {0};
strncpy ( hh, date, 2 );
strncpy ( mm, date+2, 2 );
strncpy ( ss, date+4, 2 );
strncpy ( dec, date+6, 7 );

printf("%s:%s:%s.%s\n", hh, mm, ss, dec);

[2] Short Implementation:

Either:

printf("%.2s:%.2s:%.2s.%.7s\n", date, date+2, date+4, date+6);

or:

printf("%2.2s:%2.2s:%2.2s.%7.7s\n", date, date+2, date+4, date+6);

Should work.

Instead of printf - you can use sprintf and copy to a buffer. I would also check for the correct length to avoid unpredictable behavior.

In either case - the output will be:

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