find the number of strings in an array of strings in C

前端 未结 9 1379
长发绾君心
长发绾君心 2020-12-13 18:00
char* names[]={\"A\", \"B\", \"C\"};

Is there a way to find the number of strings in the array. Like, for example in this case, it has to be 3. Ple

相关标签:
9条回答
  • 2020-12-13 18:25

    Is there a way to find the number of strings in the array

    Of course there is, try this:

    #include<stdio.h>
    
    int main(void){
        size_t size, i=0;
        int count=0;
        char* names[]={"A", "B", "C"};
    
        size = sizeof(names)/sizeof(char*);
    
        for(i=0;i<size;i++){
            printf("%d - %s\n",count+1, *(names+i));
            count++;
        }
    
        printf("\n");
        printf("The number of strings found are %d\n",count);
        return 0;
    }
    
    1 - A
    2 - B
    3 - C
    
    The number of strings found are 3
    

    Same thing with:

    #include<stdio.h>
    
    int main(void){
        size_t size, i=0;
        int count=0;
        char *names[]={"Jimmy", "Tom", "Michael", "Maria", "Sandra", "Madonna"};
    
        size = sizeof(names)/sizeof(char*);
    
        for(i=0;i<size;i++){
            printf("%d - %s\n",count+1, *(names+i));
            count++;
        }
    
        printf("\n");
        printf("The number of strings found are %d\n",count);
        return 0;
    }
    

    Output:

    1 - Jimmy
    2 - Tom
    3 - Michael
    4 - Maria
    5 - Sandra
    6 - Madonna
    
    The number of strings found are 6
    

    Or use a Function like this:

    #include<stdio.h>
    
    size_t arrLength(size_t len, char **arr){
        size_t i=0;
        size_t count=0;
    
        for(i=0;i<len;i++){
            printf("%zu - %s\n",count+1, *(arr+i));
            count++;
        }
    
        return count;
    }
    
    int main(void){
        char *names[]={"Jimmy", "Tom", "Michael", "Maria", "Sandra", "Madonna"};
        size_t length,size;
    
        size = sizeof(names)/sizeof(char*);
        length = arrLength(size, names);
    
        printf("\n");
        printf("The number of strings found are %zu\n",length);
        return 0;
    }
    

    Output:

    1 - Jimmy
    2 - Tom
    3 - Michael
    4 - Maria
    5 - Sandra
    6 - Madonna
    
    The number of strings found are 6
    
    0 讨论(0)
  • 2020-12-13 18:25
    int count = sizeof(names)/sizeof(*names);
    

    This takes the total size of names and divides it by the size of one element in names, resulting in 3.

    0 讨论(0)
  • 2020-12-13 18:31

    In this case you can divide the total size by the size of the first element:

    num = sizeof(names) / sizeof(names[0]);
    

    Careful though, this works with arrays. It won't work with pointers.

    0 讨论(0)
  • 2020-12-13 18:38

    find the number of strings in an array of strings in C

    The simple method to find the number of objects in an array is to take the size of the array divided by the size of one of its elements. Use the sizeof operator which returns the object's size in bytes.

    // For size_t
    #include <stddef.h>
    
    char* names[] = { "A", "B", "C" };
    size_t number_of_strings = sizeof names / sizeof names[0];
    
    printf("Number of strings %zu\n", number_of_strings);
    

    Important to consider the type return from sizeof is size_t, an unsigned integer type capable of indexing any array as well as the size of any object. To print a variable of type size_t, use the modifier z.

    0 讨论(0)
  • 2020-12-13 18:39

    What you're defining here isn't literally an array of strings , it's an array of pointers to characters. if you want to know the number of strings, you simply have to count the pointers, so something like

    size_t size = sizeof(names)/sizeof(char*);
    

    would do,this way you're dividing the size of the array by the size of a single pointer.But be aware this only works if the size of the array is known at compile time(i.e. the array is allocated statically), if it's defined dynamically (i.e. char** strings) then sizeof(strings) would return the size of the pointer not the array.

    p.s.:the length of the strings is not relevant, as the array isn't even "aware" of what the pointers point to.

    0 讨论(0)
  • 2020-12-13 18:40

    One simple way to return the number or strings in an array is to iterate over the array until you run out of strings. Here is an example.

    #include <stdio.h>
    
    int main()
    {
        char* names[] = {"John", "Paul", "George", "Ringo", '\0'};
    
        int size = 0;
        while(names[++size]!='\0');
    
        printf("Size: %d\n", size); // Prints "4"
    
        return 0;
    }
    

    This method has the advantage that it works even when the strings are of varying length. Note the terminating NULL byte in the names array, and the ; character to close the while statement as there is no statement body to run.

    0 讨论(0)
提交回复
热议问题