Hi,

Im having a bit of trouble with a problem:

Basically, I have an array with a set size,for example

my_array[40]

I am trying to figure out how to tell if it has less than 20 characters filled,and if it has more than 20 characters filled.

I have tried the string length function and it does not work im wondering if someone can point me in the right direction?

Many thanks.

Hi,

Im having a bit of trouble with a problem:

Basically, I have an array with a set size,for example

my_array[40]

I am trying to figure out how to tell if it has less than 20 characters filled,and if it has more than 20 characters filled.

I have tried the string length function and it does not work im wondering if someone can point me in the right direction?

Many thanks.

The strlen function will only work if your character array is a C string...i.e it ends with '\0'

>I have tried the string length function and it does not work im wondering if someone can point me in the right direction?
As StaticX has told you, you can use the strlen function only if the array is a character array, like this:

char mystring[15] = "hello";
printf("%lu\n", (unsigned long)strlen(mystring)); // will display 5

If you want to do it on any arbitrary array you'll first need to decide for yourself what you do mean with a filled subscript.
This is because when you for example declare an array like this:

int my_array[15];

space will be reserved in memory for 15 elements of type int.
That is: you'll always have 15 elements in the array, whether you assign something to them or not.
When you don't explicitly assign a value to a certain array subscript, the value of that particular element is technically undefined.

So to conclude: in standard C there's no straightforward way to find out how many subscripts you assigned a value to.
If you were using C++ I would have suggested you to use a vector, and use it's size() method, but unfortunately vectors aren't a part of C, so you can't use them.

So to conclude: in standard C there's no straightforward way to find out how many subscripts you assigned a value to.

A second variable with the number of used items is straightforward. This approach is no different from a vector in C++, except for a little extra work in maintaining the count. As long as the array is filled in a non-random manner, the count variable is easy to use:

#include <stdio.h>

int main()
{
    int a[10];
    int n = 0;
    int x;

    fputs("Enter a list of integers: ", stdout);
    fflush(stdout);

    while (n < 10 && scanf("%d", &a[n]) == 1) ++n;

    printf("There are %d integers in the list:\n", n);

    for (x = 0; x < n; ++x) printf("%d\n", a[x]);

    return 0;
}

If the array is filled at random indexes the count variable will still work, but to avoid accessing uninitialized indexes there needs to be an empty value to mark the element as empty. With that in place an algorithm similar to strlen() can also count the non-empty elements:

#include <stdio.h>
#include <ctype.h>

#define SZ 'Z'-'A'+1

int arraylen(int a[], size_t sz)
{
    int n = 0;
    int x;

    for (x = 0; x < sz; ++x)
    {
        if (a[x]) ++n;
    }

    return n;
}

int main()
{
    int a[SZ] = {0};
    int c;
    int x;

    fputs("Enter a string: ", stdout);
    fflush(stdout);

    while ((c = getchar()) != EOF)
    {
        c = toupper(c);

        if (isalpha(c)) ++a[c-'A'];
    }

    printf("There are %d unique letters\n", arraylen(a, SZ));

    for (x = 0; x < SZ; ++x)
    {
        if (a[x]) printf("%c: %d\n", x+'A', a[x]);
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.