Check string for special characters

Hello to everyone
I have some problem
I want to store esp8266 hostname in EEPROM, and load it on startup. But i need to check it for special characters to avoid reboots.
I found some function in internet, but i get instant reboots.
What am i doing wrong?

#include <EEPROM.h>
String sw_name;
char* hostname;


bool isAlphaNumerAndPeriod(char* instr){
  Serial.println("starting func");
  Serial.println(strlen(instr));
  Serial.println("starting func2");
   
      //for(int i = 0; i < strlen(instr); i++){
        for(int i = 0; i < sizeof(instr); i++){
        Serial.println("loop"+i);
           if(isalpha(instr[i])){
            Serial.println("loop1"+i);
               continue;
           }
           if(isdigit(instr[i])){
            Serial.println("loop2"+i);
               continue;
           }
           if(instr[i] == '.'){
            Serial.println("loop3"+i);
               continue;
           }
           return false;
      }
      return true;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(50);

  EEPROM.begin(1536);
  delay(50);
read_eeprom_boot();  
  Serial.println("Begin test");
  sw_name.toCharArray(hostname, sw_name.length() + 1);
  Serial.println("try func");
  if (isAlphaNumerAndPeriod(hostname)) {
    Serial.println("true");
  } else {
    Serial.println("false");
  }


  delay(50);
  Serial.println("Starting system...");


}

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

}


void read_eeprom_boot() {
  Serial.println("Reading EEPROM parameters...");


  sw_name = read_eeprom(126, 145).c_str();

}


String read_eeprom(int start_adress, int end_adress) {
  String str;
  for (int i = start_adress; i < end_adress; ++i) {
    str += char(EEPROM.read(i));
  }
  return str;
}

posting in the wrong category as a start :slight_smile:

I moved your topic to an appropriate forum category.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.

1 Like
        for(int i = 0; i < sizeof(instr); i++){

Array sizes are not passed to a function, so the above won't work as expected.

This is not valid:

        Serial.println("loop"+i);
1 Like

this won't work

you need to have enough bytes reserved into hostname (it needs to be an array) for the copy to happen.


that won't work either

sizeof(instr) is going to be 4 - that is the size of pointer.

it's not the length of your string.


best is to call your function directly with sw_name.c_str() so that you don't have to duplicate anything in memory and in the function iterate over each character until you find a null char which will denote the end of the string.

bool isAlphaNumerAndPeriod(const char* instr) {
  ...
  if (instr == nullptr) return false;
  while (* instr) { // as long as the character pointed is not null (ie we have not reached the end of the string) 
    ...

    instr++; // go to next char
  }
  ...
}

Side note: you could use isalnum()

see https://cplusplus.com/reference/cctype/

Functions

These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.

There are two sets of functions:

Character classification functions

They check whether the character passed as parameter belongs to a certain category:

Function Description
isalnum Check if character is alphanumeric
isalpha Check if character is alphabetic
isblank Check if character is blank
iscntrl Check if character is a control character
isdigit Check if character is decimal digit
isgraph Check if character has graphical representation
islower Check if character is lowercase letter
isprint Check if character is printable
ispunct Check if character is a punctuation character
isspace Check if character is a white-space
isupper Check if character is uppercase letter
isxdigit Check if character is hexadecimal digit
tolower Convert uppercase letter to lowercase
toupper Convert lowercase letter to uppercase
1 Like

Oh, i am sorry. I will