Go Down

Topic: Character array (check for NON-allowed values) (Read 104 times) previous topic - next topic

877

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:


Code: [Select]

  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!
https://www.tutorialspoint.com/c_standard_library/c_function_strpbrk.htm


jremington

#1
Dec 14, 2019, 10:37 pm Last Edit: Dec 14, 2019, 10:39 pm by jremington
Could you more carefully explain what you are trying to do?  Your description seems to have conflicting requirements.

For example:
Quote
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?

877

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?

Paul_KD7HB

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

dougp

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

Code: [Select]

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:
}
I don't trust atoms.  They make up everything.

No private consultations undertaken!

jremington

#5
Dec 15, 2019, 02:05 am Last Edit: Dec 15, 2019, 02:06 am by 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().

877

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


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.

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.

Go Up