Character array (check for NON-allowed values)

Hi,

I need to filter a string to check for NON-allowed values, so it can be stopped instead of passed onto another function.

Here is the section of code I created:

  char *button = "01234xx";                  //incoming variable from iOS app
  char *allowed = "01234567890corps";   //an array listing the allowed characters
  char *ret;                                            // a true of false placeholder for checking allowed characters
  ret = strpbrk(button, allowed);              //check whether the string finds any non-allowed character

  Serial.print("Allowed: ");
  Serial.println(ret);

Problem is it will let through anything with an allowed character (0123456789corps).
Maybe I am using the wrong function.

I actually want it to check for a character that's NOT in 'char button' and give a true or false.
So I can create some if / else code based on that. I put in the serial print just to see what was happening with 'ret'.

Here's where I have been looking, but got myself tired and confused!

Could you more carefully explain what you are trying to do? Your description seems to have conflicting requirements.

For example:

I actually want it to check for a character that's NOT in 'char button'

If button is a template, why is 'x' present twice?

Hi there,

Apologies if it wasn’t clear. I’ll try and explain further.

The incoming string “button” is coming from the Blynk iOS app. Basically in the app’s terminal windows I type in a string and press send, and it gets written to the Arduino variable “button”. It can be any length and any type-able character.

Going from there, in the sketch, the string is separated into the individual characters, converted to a binary ‘pulse’ and sent one bit at a time to a single digital output.

The system connected to the Arduino digital output only understands certain characters.
Hence the need to check for a “bad” string, i.e one not containing only the characters 0123456789corps.

Examples of good strings (containing any combination of 0123456789corps):
1234p5
1234o6
123456corp
p1234o5p6
and so on..

Example of a bad string:
01234xx
12345something
and so on..

Is this clearer?

Still not clear at all. Are you saying that the string can ONLY contain the digits 0 through 9 and the characters "corps"?

Look at the function "isDigit()" to examine one of the characters. If true, then you are done if false then check for the individual character,'c', if false the 'o', if false, the 'r', and so on.

Write the test as a function and pass a pointer to the character in question and have the function return a boolean as true or false.

Paul

A bit crude but, based on my understanding this seems to do the right thing.

char *button = "01234xx";   //incoming variable from iOS app
char *button2 = "3591spoc"; // test strings
char *button3 = "5a55g";
char *button4 = "m1sf1t";
char *button5 = "667r313op";
//
char *allowed = "01234567890corps";   //an array listing the allowed characters
byte goodString; // legal character counter

void setup() {
  Serial.begin(115200);
  goodString = 0;
  for (byte i = 0; i < strlen(button5); i++) {
    if (strchr(allowed, button5[i]))
      goodString++;
      else break;
  }
  if (goodString == strlen(button5)) Serial.println("string passed!");
  else Serial.println("bad string!");
}

void loop() {
  // put your main code here, to run repeatedly:
}

Every time a character comes in, you could check it for a non-allowed character using the function memchr(), or as suggested above, the function strchr().

Apologies if I have not been clear everyone, it's difficult to put into words sometimes..
Thanks for the help and patience.

dougp:
A bit crude but, based on my understanding this seems to do the right thing.

It doesn't seem crude to me, I can just about understand that at my current experience level!
Thank you it does work perfectly.

jremington:
Every time a character comes in, you could check it for a non-allowed character using the function memchr(), or as suggested above, the function strchr().

Thanks I will have a look at that for future reference.