How to use length() with char datatype?

Dear all

Sorry for my beginner’s question: I am struggling with the lenght() function with my little demo sketch below receiving the error msg: request for member ‘lenght’ in ‘mytext[text_id]’, which is of non-class type ‘char*’

How can I assign the number of characters to a int variable called text_lenght in my example?

Thank you very much for your help.

#include <Arduino.h>

int text_id = 2; //for example to count the number of characters in my second string below 

char *my_text[] = {"This is my long text with more than fourty characters",
                   "This is another sample text",
                   "Some more text to count",
                   "My short text"};

//==========================================================================================
void setup()
{
  Serial.begin(115200);  

  int text_lenght = (my_text[text_id].lenght());

  Serial.print("Number of characters in my second sample text: ");
  Serial.println(text_lenght);
}


//==========================================================================================
void loop()
{
}

For strings use the strlen() function.

#include <Arduino.h>

int text_id = 2; //for example to count the number of characters in my second string below

char *my_text[] = {"This is my long text with more than fourty characters",
                   "This is another sample text",
                   "Some more text to count",
                   "My short text"};

//==========================================================================================
void setup()
{
  Serial.begin(115200); 

  int text_lenght = strlen(my_text[text_id]);

  Serial.print("Number of characters in my second sample text: ");
  Serial.println(text_lenght);
}


//==========================================================================================
void loop()
{
}

Dear groundFungus

works just perfect. Many thanks.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.