Reading individual characters in a string

I recently started learning to code, and I was thinking to make something that would read a string, and then convert it to morse code that would be displayed via LED. I couldn't find any documentation on a function that could read a string and individually read the characters within, so I was wondering if anyone had any suggestions or ideas on how I could approach this.

I'm relatively new to this, so expect me to ask for clarification. :slight_smile:

Thanks in advance!

char txt = "hello world";

void setup()
{
  Serial.begin(57600);

  for (byte cnt = 0; cnt < strlen(txt); cnt++)
  {
    Serial.print("char at position ");
    Serial.print(cnt);
    Serial.print(" = ");
    Serial.println(txt[cnt]);
  }
}

voud loop()
{
}

Not compiled or tested.

Or if you want a compileable version of that :wink:

sterretje:
Not compiled or tested.

char txt[] = "hello world"; //xxxxxx added []

void setup()
{
  Serial.begin(57600);

  for (byte cnt = 0; cnt < strlen(txt); cnt++)
  {
    Serial.print("char at position ");
    Serial.print(cnt);
    Serial.print(" = ");
    Serial.println(txt[cnt]);
  }
}

void loop() //xxxxxxx not voud
{
}

lindsayBoxer:
Or if you want a compileable version of that :wink:

Stupid mistake; thanks for correcting.

I was interested to see that "strlen" returns the length excluding the null. I usually use the sizeof(array)/sizeof(an element) approach, which includes the null.

sterretje:
Stupid mistake; thanks for correcting.

I spent about half an hour earlier trying to figure out why a specific Serial.print was cluttering up my output when I thought was 1000% sure it was if()'d out. I had broken my own golden rule of not having more than one statement on a line, and when I stuck the if in at the front, I forgot the {}. It's easy to make stupid mistakes.