Simon_A
Simon_A

Reputation: 137

Check for particular characters in array in C

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?

Upvotes: 1

Views: 127

Answers (8)

user411313
user411313

Reputation: 3990

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");

Upvotes: 0

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

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;
    }
}

Upvotes: 0

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27632

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");
}

Upvotes: 3

ShinTakezou
ShinTakezou

Reputation: 9671

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;

Upvotes: 0

Dmytro Sirenko
Dmytro Sirenko

Reputation: 5083

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

Upvotes: 1

chipmunk
chipmunk

Reputation: 970

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

Upvotes: 1

Intrepidd
Intrepidd

Reputation: 20878

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.

Upvotes: 2

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

Upvotes: 1

Related Questions