Thread: Validation of string format

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    19

    Validation of string format

    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.

    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;
    If the flag is 1 by the end, the validation has failed.

    I looked up regular expression matching for C but it confused the crap out of me :-\

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You could get creative with strtok(), perhaps.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    In pseudocode, you could do this:
    Code:
    while true
      read ampersand (if not; fail)
      read integer (if not, fail)
      if we read the entire string, succeed
      read comma (f not, fail)
      repeat
    Just a few lines of code...

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    The numbers are in character format so reading an integer wouldn't quite work would it?

    As x,y or z could be 3, 15 and 2318493, for example.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Okay, so let me reword that one for you:
    read all characters 0-9.

    I can't imagine why reading an integer wouldn't work in that case, except for an overflow. Of course you can't just treat the character array as an integer, but if you read an integer from standard input that doesn't mean you treat it as a character array either. The integer is read from a source; converted, but still read.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can try the scanset funtionality of sscanf() to parse 'n store the string, for ex.
    Code:
    char a[2], b[20], c[2], d[2], e[20], f[2], g[2], h[20];
    char s[] = "&123456,&1007,&9";
    
    if (sscanf(s, "%[&]%[0-9]%[,]%[&]%[0-9]%[,]%[&]%[0-9]", a, b, c, d, e, f, g, h) == 8)
        printf("okay\n");

    Edit: only caveat is that the arrays that store numbers have to be allocated beforehand.
    Last edited by itCbitC; 05-02-2009 at 05:36 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by itCbitC View Post
    You can try the scanset funtionality of sscanf() to parse 'n store the string, for ex.
    Code:
    char a[2], b[20], c[2], d[2], e[20], f[2], g[2], h[20];
    char s[] = "&123456,&1007,&9";
    
    if (sscanf(s, "%[&]%[0-9]%[,]%[&]%[0-9]%[,]%[&]%[0-9]", a, b, c, d, e, f, g, h) == 8)
        printf("okay\n");

    Edit: only caveat is that the arrays that store numbers have to be allocated beforehand.
    That, and the fact that you have a limit of number of items. And what, exactly, is wrong with my suggestion?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nothing. It's a good way to do it. A lot of us here just enjoy finding more than one way to solve a problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by quzah View Post
    Nothing. It's a good way to do it. A lot of us here just enjoy finding more than one way to solve a problem.


    Quzah.
    Which is a good thing. If the other solution has even the slightest advantage

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM