Paul
Paul

Reputation: 15

C - How to check if unwanted character is in an array?

I have a question about string arrays.

I am trying to find a way to check if a string has an undesired character in it.

Tested array has to only contain one of these 8 options: {'A','a','C','c','G','g','T','t'}

All the white spaces are removed prior to this checking process.

If I have two arrays A = {AGctcgtacgtacg} B = {CGTAagctFcg}

A should pass the test and B should fail because it has 'F' in it.

I'm thinking of using a for loop to go over each element of the array and trying to check if the character is one of the options.

const char test[] = {'A','a','C','c','G','g','T','t'};
int l = strlen(A);
char A[] = {AGctcgtacgtacg} 
char B[] = {CGTAagctFcg}
for (i = 0; i < l; i++){
    if (A[i] is one of the characters in test){
        do nothing... (keep checking)
    }
    else{
    break;
    printf("Error: the array contains unwanted character.");
    exit(1);
    }
}

Upvotes: 1

Views: 1793

Answers (2)

cleblanc
cleblanc

Reputation: 3688

Try using strchr(...) NOTE that test needs to be NUL terminated

const char test[] = {'A','a','C','c','G','g','T','t','\0'};
int l = strlen(A);
char A[] = {AGctcgtacgtacg} 
char B[] = {CGTAagctFcg}  
for (i = 0; i < l; i++){
    if (strchr(test, A[i])){
        do nothing... (keep checking)
    }
    else{
        break;
        printf("Error: the array contains unwanted character.");
        exit(1);
    }
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

You can use standard C function strspn declared in header <string.h> to check whether a string contains only valid characters.

For example

#include <stdio.h>
#include <string.h>

int main( void )
{
    const char *test = "AaCcGgTt";
    char A[] = "AGctcgtacgtacg";
    char B[] = "CGTAagctFcg";

    if (A[strspn(A, test)] == '\0')
    {
        puts("Array A has valid characters");
    }
    else
    {
        puts("Array A has invalid characters");
    }

    if (B[strspn(B, test)] == '\0')
    {
        puts("Array B has valid characters");
    }
    else
    {
        puts("Array B has invalid characters");
    }
}

The program output is

Array A has valid characters
Array B has invalid characters

If you need to know also the position of the first invalid character then you can write for example

#include <stdio.h>
#include <string.h>

int main()
{
    const char *test = "AaCcGgTt";
    char A[] = "AGctcgtacgtacg";
    char B[] = "CGTAagctFcg";
    size_t n;

    if (A[n = strspn(A, test)] == '\0')
    {
        puts("Array A has valid characters");
    }
    else
    {
        puts("Array A has invalid characters");
        printf("Invalid character is %c at position %zu\n", A[n], n);
    }

    if (B[n = strspn(B, test)] == '\0')
    {
        puts("Array B has valid characters");
    }
    else
    {
        puts("Array B has invalid characters");
        printf("Invalid character is %c at position %zu\n", B[n], n);
    }
}

The program output is

Array A has valid characters
Array B has invalid characters
Invalid character is F at position 8

Upvotes: 1

Related Questions