3

I need to Validate the string . The string has format like "V02:01:02:03:04".
What I am trying to do :
1) I want to check correct length is provided
2) First character is 'V'
3) All other characters are 2 letter digits and separated by colon and at correct position.
4) There is no blank space tabs et.c
4) Cant think of any more validation .

What I have done so far
1) Function has easily check len and fist char not verify that subsequent chars after first does not contain alpha or space. But I am bit stuck about how to check colon positions and 2 letter char after every colon.

Below is my attempt.

 int firmwareVerLen = 15;
const std::string input = "V02:01:02:03:04"; // this is the format string user will input  with changes only in digits.
bool validateInput( const std::string& input )
{
    typedef std::string::size_type stringSize;
    bool result = true ;
    if( firmwareVerLen != (int)input.size() )
    {       
      std::cout <<" len failed" << std::endl;
      result = false ;
    }

    if( true == result )
   {

        if( input.find_first_of('V', 0 ) )
        {
             std::cout <<" The First Character is not 'V' " << std::endl;
             result = false ;
         }
   }

   if( true == result )
   {
        for( stringSize i = 1 ; i!= input.size(); ++ i )
       {
            if( isspace( input[i] ) )
           {
               result = false;
               break;
           }
           if( isalpha(input[i] ) )
            {
               cout<<" alpha found ";
               result = false;
               break;
            }

             //how to check further that characters are digits and are correctly  separated by colon
        }
    }

   return result;
 }
samantha
  • 565
  • 3
  • 13
  • 29

3 Answers3

2

If it's such a strict validation, why not check character by character? for example (given you've done size already)

  if (input[0] != 'V')
    return false;
  if (!std::isdigit(input[1]) || !std::isdigit(input[2]))
    return false;
  if (input[3] != ':')
    return false;
  // etc...
Nim
  • 33,299
  • 2
  • 62
  • 101
0

Starting with the very first digit, all characters must be digits except if there position modulo 3 is 0, than it must be a colon.

HTH Torsten

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
0

For such a short short input and strict format, I'd probably do something like this:

bool validateInput( const std::string& input )
{
    static const size_t digit_indices[10] = {1, 2, 4, 5, 7, 8, 10, 11, 13, 14};
    static const size_t colon_indices[4] = {3, 6, 9, 12};
    static const size_t firmwareVerLen = 15;

    static const size_t n_digits = sizeof(digit_indices) / sizeof(size_t);
    static const size_t n_colons = sizeof(colon_indices) / sizeof(size_t);

    if (input.size() != firmwareVerLen) return false;     // check 1)
    if (input[0] != 'V') return false;                    // check 2)

    for (size_t i = 0; i < n_digits; ++i)                       // check 3)
        if (!is_digit(input[digit_indices[i]]) return false;

    for (size_t i = 0; i < n_colons; ++i)                        // check 3)
        if (input[colon_indices[i]] != ':') return false;

    // check 4) isn't needed

    return true;
}

Reasonably easy to mantain if input format changes, IMO.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • Thanks a jrok, your solution is fab .. not only it serve my purppose but also helped me to learn something new . thanks a ton.. – samantha Jul 13 '12 at 08:46