I have this C code fully working:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
int isAlphaNum(char *str) {
for (int i = 0; str[i] != '\0'; i++)
if (!isalnum(str[i]))
return 0;
return 1;
}
int main() {
char *user_string = "abcdedf0123456789ABCD";
if (isAlphaNum(user_string)) {
printf(" is valid \n");
} else {
printf(" is not valid \n");
}
printf(" \n end \n");
return 0;
}
the following is copied from terminal:
but when I receive input via socket like this:
90a41ae8477a334ba609e06cujdikj#%&%$@$Dkdfsノ,ᅵハ"]モ {ᆳf
or
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒814
the program crashes at this part:
for (int i = 0; str[i] != '\0'; i++)
if (!isalnum(str[i]))
I used the function by @chqrlie and works: edited
int isAlphaNum(const char *str) {
//this message is printed , then craches
printf("pass isAlphaNum userinput = %s\n" , str);
while (*str) {
if (!isalnum((unsigned char)*str++))
return 0;
}
return 1;
}
if (isAlphaNum(userinput)) {
printf(" success ;) \n");
}
all ok now thanks for the help