Split and send a string one character at a time

I want to know how to split a variable up into it's individual parts.
The variable can be between 4 and 6 characters long, and is alphanumeric.

e.g.

void processData()
{

char pass = "12345A";

<some code here to determine length of string>

<length = "number of characters"; >

<for loop? to iterate through each character (using length variable) and send to function printpass()  >
}


void printpass()
{

Serial.println(character);

}

Any hints on how to do this?

You need to define your string in an array of chars

char pass [] = "12345A";

Then you can use several functions related to strings

As an example:

char pass [] = "12345A";

void setup() {
  Serial.begin(115200);
  Serial.println(pass);
  int len = strlen(pass);
  Serial.println(len);
  for (int i=0;i<len;i++) Serial.println(pass[i]);
}

void loop() {
 
}

This displays:

12345A
6
1
2
3
4
5
A

@lesept thanks that is what I was trying to formulate!

Thanks for the link too, I was just looking on the Arduino pages and reading about strings, lots of useful functions: https://www.arduino.cc/en/Tutorial/BuiltInExamples#strings

:slight_smile:

That link takes you to a page about Strings (objects created using the String library). lesept used strings (arrays of chars terminated by a zero)

The former are not well regarded in the small memory footprint of the average Arduino and should be used with caution, if at all, whilst the latter are not memory hogs and are preferred

Thanks UKHeliBob, I was wondering about that. As I read that strings are not preferred for Arduino as they can cause memory issues.

That clears it up :wink:

Be careful with your use of the words string and String

strings (lowercase s) are to be preferred in the Arduino small memory environment

Hmm having an daft issue here.

As I'm using BLYNK_WRITE it only support Strings

String pass = param.asStr();

So I of course need convert it to a character array. I could use for example "pass.toCharArray(button, 10)”
but it seems to need the array length setting, which I don't want.

Maybe I could use something like:

// assigning value to string s 

    string s = "geeksforgeeks"; 

  

    int n = s.length(); 

  

    // declaring character array 

    char char_array[n + 1]; 

  

    // copying the contents of the 

    // string to char array 

    strcpy(char_array, s.c_str());

I just wonder if there’s a better way?

877:
As I'm using BLYNK_WRITE it only support Strings

According to: Documentation for Blynk, the most popular IoT platform for businesses. it supports:

param.asInt();
param.asFloat();
param.asDouble();
param.asStr();

You could also try:

char *buff = (char *) param.getBuffer();

This should give you a pointer to the raw character buffer which should be a null-terminated c-string.

Nice! I didn't see those other options of buffer/length.

char *buff = (char *) param.getBuffer();

This works for me, I'm not 100% sure how it works?