Question

How to check if a string contains non alphanumeric values? I want to create a condition to return if match one non alphanumeric character.

    void checkTag(char tag[]){
    if(strlen(tag) == 0) return;
    if(strlen(tag) == 1) return;
    if(strlen(tag) == 2) return;
    if(strlen(tag) == 3) return;

In Serial Monitor is showed:

    À¨À¨Àª®)
Was it helpful?

Solution

for (int i=0;i<strlen(tag); i++){
    if ( (tag[i] >= 'a' && tag[i] <= 'z') || (tag[i] >= 'A' && tag[i] <= 'Z') || (tag[i] >= '0' && tag[i] <= '9'){
        //this char is OK
    }else{
        return;
    }
}
//if you are here tag is valid

edit: changed the comments to be sure you don't put code in the if

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top