Home > Net >  How do I get the length of each row of the char** in C?
How do I get the length of each row of the char** in C?

Time:07-04

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.

CodePudding user response:

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
}

CodePudding user response:

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;
}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related