Say I have a string in the format "&x,&y,&z", is there a quicker way of validating it for 'correctness'
I'm trying to make a program which accepts the string in that format (Ampersand, integer of any length, comma, ampersand, integer of any length, comma, ampersand, integer of any length) and I've succeeded... but it's a state machine and the code is very very long and cumbersome.
If the flag is 1 by the end, the validation has failed.Code:if (opcheck[0] == '&') { current = 'b'; if (isdigit(opcheck[i])){i++; current = 'c';} else flag = 1; while (current == 'c') { if (isdigit(opcheck[i])) { i++; } else if (opcheck[i] == ','){i++; current = 'd';} else {flag = 1; break;} } while (current == 'd') { if (opcheck[i] == '&'){i++; current = 'e';} else {flag = 1; break;} } while (current == 'e') { if (isdigit(opcheck[i])){i++; current = 'f';} else {flag = 1; break;} } while (current == 'f') { if (isdigit(opcheck[i])) { i++; } else if (opcheck[i] == ','){i++; current = 'g';} else {flag = 1; break;} } while (current == 'g') { if (opcheck[i] == '&'){i++; current = 'h';} else {flag = 1; break;} } while (current == 'h') { if (isdigit(opcheck[i])){i++; current = 'i';} else {flag = 1; break;} } while (current == 'i') { if (isdigit(opcheck[i])) { i++; } else if (opcheck[i] == '\0'){i++; current = 'j';} else {flag = 1; break;} } } else flag = 1;
I looked up regular expression matching for C but it confused the crap out of me :-\