Find the number of items in an array of Strings

Hi everyone

I have a project that generates random sentences. There are three String arrays that contain words and they are printed to an LCD in a standard template.

Right now I'm having to manually count how many elements I have in each array so I can set the upper limit for the random number generator. I thought I could use sizeof() but that just returns the number of bytes, not the number of String items in the array.

Is there a function I can use to achieve this? Or if there is not one build in how can I write one?

Many thanks

Maybe show us some examples of the arrays.

Using the String class can cause hard to find bugs caused by memory corruption. Better to use null terminated character arrays (strings).

Show us your use of sizeof().

The number of elements in an array is the number of bytes in the array, divided by the size of a single element.

sizeof (array) / sizeof (array [0])
#include "Arduino.h"

int getArrayLen();
int getArrayLen(String array[]);


String cmds[] = { "c1","c22", "c333", "c4444", "c55555"};


void setup() {
   Serial.begin(9600);
   Serial.println((String)"getArrayLen(): "+getArrayLen());   // Correctly returns 5
   //
   Serial.println(getArrayLen(cmds));

}

// The loop function is called in an endless loop
void loop() {
   //Add your repeated code here
}

// Generic function
int getArrayLen() {
   return (int)sizeof(cmds)/sizeof(cmds[0]);
}


// Specific function
int getArrayLen(String array[]) {
   int arbitrary = 10;
   for (int i = 0; i < arbitrary; ++i) {
      Serial.println((String)"array: "+ *array);
      array = array +1;
   }
   return 0;
}

These are slightly modified versions of the arrays I have. The elements aren't all the same size so I can't use sizeof (array) / sizeof (array[0])

String commands[] { "Go away",
                    "Leave now",
                    "Bog off"};
                    
String descriptor[] { "silly",
                      "daft",
                      "naughty",
                      "dirty"};
                      
String insult[] { "thing",
                  "person"};

I have tried the following but it just returns the number of bytes.

Serial.print(sizeof(insult));

Don't use code to find the length. That uses memory. sizeof() operates at compile time, and generates no code. Instead it produces a constant that the compiler can use in place of a programmer supplied constant. Thus there is no memory usage penalty.

pianomatt:
These are slightly modified versions of the arrays I have. The elements aren't all the same size so I can't use sizeof (array) / sizeof (array[0])

String commands[] { "Go away",

"Leave now",
                    "Bog off"};
                   
String descriptor[] { "silly",
                      "daft",
                      "naughty",
                      "dirty"};
                     
String insult[] { "thing",
                  "person"};




I have tried the following but it just returns the number of bytes.



Serial.print(sizeof(insult));

Sure you can!

String insult[] { "thing",
                  "person"};
                   size_t INSULTSIZE = sizeof ( insult) / sizeof ( insult[]);

TheMemberFormerlyKnownAsAWOL:
The number of elements in an array is the number of bytes in the array, divided by the size of a single element.

sizeof (array) / sizeof (array [0])

Okay, this works! Thanks.

I don't understand how it works though, because each element is potentially a different length. I have some with only 3 letters, others much longer.

sizeof String is a constant - six, IIRC.