1

I would like to check for certain characters in an array at certain positions.

The array starts with $$$$ then has eight characters then another $, eight more characters and finishes with $$$$. For example char my_array[50] = "$$$$01FF4C68$02543EFE$$$$";

I want to check that all the positions where there are supposed to be $ do have them.

I could split the array into the three parts that contain the characters and then test for them separately but is there a better way of doing this?

2
  • 1
    Something like strstr? Commented Dec 2, 2012 at 19:53
  • So it's always $$$$ + 8 + $ + 8 + $$$$? If so, just use a loop and keep track of the position. Commented Dec 2, 2012 at 19:58

8 Answers 8

3

Why complicate things?

if (my_array[0] != '$'
    || my_array[1] != '$'
    || my_array[2] != '$'
    || my_array[3] != '$'
    || my_array[12] != '$'
    || my_array[21] != '$'
    || my_array[22] != '$'
    || my_array[23] != '$'
    || my_array[24] != '$')
{
    printf("Wrong!\n");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use strstr()

To check if the array begins with eight $ : strstr(my_array, "$$$$$$$$")

To check if the array ends with eight $ : strstr(my_array + 16, "$$$$$$$$")

The +16 is here to shift the pointer so the beginning of my_array + 16 will be the place were the $ are supposed to be.

Comments

1

You might want to use the strstr functinn to find the $$$....

Comments

1

yes there is, you might want to use Regular Expressions, Please read http://www.peope.net/old/regex.html

Comments

1

If you use POSIX-compatible platform and some more complex patterns are about to emerge in your code, you can take a look at regular expressions, e.g. PCRE

Comments

0

You could also avoid using strstr since the format is simple and fixed; until the example format holds::

 bool ok = strlen(my_array) >= 25 /* just be sure there are at least all expected chars */ &&
           strncmp(my_array, "$$$$", 4) == 0 &&
           strncmp(my_array + 12, "$", 1) == 0 /* my_array[12] == '$' */&&
           strncmp(my_array + 21, "$$$$", 4) == 0;

Comments

0

A long option without using the string.h library is, make 3 tests:

#include <stdio.h>

int firstTest( char a[] );
int secondTest( char a[] );
int thirdTest( char a[] );

int main (void)
{
    int result;
    char my_array[50] = "$$$$01FF4C68$02543EFE$$$$";
    if( ( firstTest( my_array ) == 1 ) && ( secondTest( my_array ) == 1 ) && ( thirdTest( my_array ) == 1 ) ){
        printf( "The string is valid.\n" );
        result = 1;
    }
    else{
        printf( "The string is invalid.\n" );
        result = 0;
    }

    return 0;
}

int firstTest( char a[] )
{
    int i;

    for( i = 0; i < 4; i++ ){
        if ( a[i] != '$' ){
            return 0;
            break;
        }
        return 1;
    }
}

int secondTest( char a[] )
{
    if( my_array[12] != '$' )
        return 0;
    else
        return 1;
}

int thirdTest( char a[] )
{
    int i;

    for( i = 21; i < 25; i++ ){
        if ( a[i] != '$' ){
            return 0;
            break;
        }
        return 1;
    }
}

Comments

0

sscanf should do the work

  char my_array[50] = "$$$$01FF4C68$02543EFE$$$$";
  int n,m;
  if( !sscanf(my_array,"$$$$%*8[0-9A-H]%n$%*8[0-9A-H]$$$$%n",&n,&m) && n==12 && m==25 )
    puts("ok");
  else
    puts("not ok");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.