如何在C中获得Char **的每一行的长度?

发布于 2025-02-12 09:01:24 字数 374 浏览 0 评论 0原文

例如:

char* arr = "x    xxxx\n"
            "x xx\n"
            "x  xxx\n";

我尝试获取每一行的长度。我尝试了sizeof,strlen,它们都没有。 我只能得到总尺寸,例如:

int size = 0;
while(*arr) {
  size++;
  arr++;
}

现在,我只知道“ ARR”行的大小。我正在尝试找到每一行的长度。如果整行的整个长度相等,我可以找到它。 但是,如果它们与众不同怎么办?我应该如何处理这个?还是C允许这样做? 我试图想到Malloc,Realloc,但不确定这对此有用。请给我一些提示。 谢谢。

For instance:

char* arr = "x    xxxx\n"
            "x xx\n"
            "x  xxx\n";

I try to get the length of each row. I try sizeof, strlen, none of them works.
I only can get the total size, like:

int size = 0;
while(*arr) {
  size++;
  arr++;
}

Right now, I only know the size of row of "arr". I'm trying to find the length of each row. I can find it if the whole thing has equal length for each row.
But what if they are different? How should I approach this one? Or does C allow to do such things?
I was trying to think of malloc, realloc, but not sure that would work for this one. Please give me some hints.
Thank you.

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

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

发布评论

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

评论(4

江湖彼岸 2025-02-19 09:01:24

尝试此(请记住#include< string.h>

while (*arr) {
    size_t len = strcspn(arr, "\n");

    // use len and arr per your requirements
    work_with_data(arr, len);

    arr += len; // advance to the '\n' (or '\0')
    if (*arr) arr++; // if there really was an '\n' skip it
}

Try this (remember to #include <string.h>)

while (*arr) {
    size_t len = strcspn(arr, "\n");

    // use len and arr per your requirements
    work_with_data(arr, len);

    arr += len; // advance to the '\n' (or '\0')
    if (*arr) arr++; // if there really was an '\n' skip it
}
行雁书 2025-02-19 09:01:24

问题中定义的字符串不是char **,它是带有嵌入式新线的单个C字符串。

您可以使用简单的迭代来计算其包含的行的长度:

#include <stdio.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    size_t i, start, line = 1;

    for (i = start = 0; arr[i] != '\0'; i++) {
        if (arr[i] == '\n') {
            printf("The length of line %zu is %zu.\n", line, i - start);
            start = i + 1;
            line++;
        }
    }
    /* special case if the last line does not end with a newline */
    if (i > start) {
        printf("The length of line %zu is %zu.\n", line, i - start);
    }
    return 0;
}

您可以使用strchr()strcspn()来简化循环,该 strcspn()接受一个分隔器的字符串:

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

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p, *start;
    size_t line = 1;

    for (start = arr; (p = strchr(start, '\n')) != NULL; start = p + 1) {
        printf("The length of line %zu is %td.\n", line, p - start);
        line++;
    }
    /* special case if the last line does not end with a newline */
    if (*start) {
        printf("The length of line %zu is %zu.\n", line, strlen(start));
    }
    return 0;
}

使用strcspn()允许将特殊情况折叠到主循环中:

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

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p = arr;
    size_t line = 1;

    while (*p != '\0') {
        size_t len = strcspn(p, "\n");
        printf("The length of line %zu is %zu.\n", line, len);
        line++;
        p += len + (p[len] != '\0');
    }
    return 0;
}

The string defined in the question is not a char **, it is a single C string with embedded newlines.

You can compute the lengths of the lines it contains with a simple iteration:

#include <stdio.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    size_t i, start, line = 1;

    for (i = start = 0; arr[i] != '\0'; i++) {
        if (arr[i] == '\n') {
            printf("The length of line %zu is %zu.\n", line, i - start);
            start = i + 1;
            line++;
        }
    }
    /* special case if the last line does not end with a newline */
    if (i > start) {
        printf("The length of line %zu is %zu.\n", line, i - start);
    }
    return 0;
}

You can simplify the loop using strchr() or strcspn(), which accepts a string of separators:

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

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p, *start;
    size_t line = 1;

    for (start = arr; (p = strchr(start, '\n')) != NULL; start = p + 1) {
        printf("The length of line %zu is %td.\n", line, p - start);
        line++;
    }
    /* special case if the last line does not end with a newline */
    if (*start) {
        printf("The length of line %zu is %zu.\n", line, strlen(start));
    }
    return 0;
}

Using strcspn() allows for the special case to be folded into the main loop:

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

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p = arr;
    size_t line = 1;

    while (*p != '\0') {
        size_t len = strcspn(p, "\n");
        printf("The length of line %zu is %zu.\n", line, len);
        line++;
        p += len + (p[len] != '\0');
    }
    return 0;
}
横笛休吹塞上声 2025-02-19 09:01:24

您可以使用函数 strchr 在字符串中查找Newline字符,并使用该信息来计算单个行的长度:

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

int main( void )
{
    char *arr = "x    xxxx\n"
                "x xx\n"
                "x  xxx\n";

    char *p = arr;

    for ( int i = 1; ; i++ )
    {
        char *q;

        q = strchr( p, '\n' );

        if ( q != NULL )
        {
            printf( "The length of line %d is %td.\n", i, q-p );
            p = q + 1;
        }
        else
        {
            size_t len = strlen( p );

            if ( len != 0 )
            {
                printf( "The length of line %d is %zu.\n", i, len );
            }

            break;
        }
    }
}

此程序具有以下输出:

The length of line 1 is 9.
The length of line 2 is 4.
The length of line 3 is 6.

You can use the function strchr to find the newline characters in the string, and use that information for calculating the length of the individual lines:

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

int main( void )
{
    char *arr = "x    xxxx\n"
                "x xx\n"
                "x  xxx\n";

    char *p = arr;

    for ( int i = 1; ; i++ )
    {
        char *q;

        q = strchr( p, '\n' );

        if ( q != NULL )
        {
            printf( "The length of line %d is %td.\n", i, q-p );
            p = q + 1;
        }
        else
        {
            size_t len = strlen( p );

            if ( len != 0 )
            {
                printf( "The length of line %d is %zu.\n", i, len );
            }

            break;
        }
    }
}

This program has the following output:

The length of line 1 is 9.
The length of line 2 is 4.
The length of line 3 is 6.
夕色琉璃 2025-02-19 09:01:24

字符串的每个“行”以新的行字符'\ n'结束。

char* arr = "x    xxxx\n"
            "x xx\n"
            "x  xxx\n";

因此,要确定一行的长度,您需要找到的是找到新行字符的位置。可以使用标准字符串函数strchr完成。

这是一个演示程序。

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

int main( void )
{
    char* arr = "x    xxxx\n"
                "x xx\n"
                "x  xxx\n";    

    for ( const char *start = arr; *start; )
    {
        const char *end = strchr( start, '\n' );

        if ( end  == NULL )
        {
            end = start +strlen( start );
        }

        printf( "The row \"%.*s\" has the length %td\n",
                ( int )( end - start ), start, end- start );

        start = *end ? end + 1 : end;                
    }                
}

程序输出是

The row "x    xxxx" has the length 9
The row "x xx" has the length 4
The row "x  xxx" has the length 6

行的长度的程序输出,未考虑新的行字符。如果您需要计算它,只需增加长度的值即可。

Each "row" of your string literal ends with the new line character '\n'.

char* arr = "x    xxxx\n"
            "x xx\n"
            "x  xxx\n";

So to determine the length of a row what you need is to find the position of the new line character. That can be done using the standard string function strchr.

Here is a demonstration program.

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

int main( void )
{
    char* arr = "x    xxxx\n"
                "x xx\n"
                "x  xxx\n";    

    for ( const char *start = arr; *start; )
    {
        const char *end = strchr( start, '\n' );

        if ( end  == NULL )
        {
            end = start +strlen( start );
        }

        printf( "The row \"%.*s\" has the length %td\n",
                ( int )( end - start ), start, end- start );

        start = *end ? end + 1 : end;                
    }                
}

The program output is

The row "x    xxxx" has the length 9
The row "x xx" has the length 4
The row "x  xxx" has the length 6

The program output of the length of a row does not take into account the new line character. If you need to count it just increase the value of the length.

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