I am new to the world of C but am enthusiastic nonetheless, so pardon me if this appears to be a particularly basic question...

I am attempting to write a program that uses DNA strands.. which consist of A,T,C,and G characters...

The user is prompted to input a DNA strand.

After they have input the string of a certain length I want to use a user defined function to check the string and ensure the fact that it doesn't contain any letters that are not A,T,C, or G. If it does contain an invalid character I want to alert the User... If it does not contain any characters other than A,T,C, or G then there will be no problem and the program will continue...

any help regarding this would be greatly appreciated.

David

Dani AI

Generated

A simple, robust approach is to normalize the input (strip trailing newline, collapse case) and check characters one at a time so you can report which character and where. As suggested, a loop is clear and easy to extend; correctly noted there are compact library one‑liners if you prefer them; and 's reminder about headers is worth keeping in mind. The example below shows a safe C routine that reports the index of the first invalid character and handles common input pitfalls.

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

int first_invalid(const char *s) {
    for (size_t i = 0; s[i] != '\0'; ++i) {
        unsigned char uc = (unsigned char)s[i];      /* avoid UB for negative char */
        char c = (char) toupper(uc);
        if (c != 'A' && c != 'T' && c != 'C' && c != 'G')
            return (int)i;
    }
    return -1;
}

/* Usage: read with fgets, remove newline, then validate */

For quick validation in Python the same idea is very concise and readable:

def first_invalid(s):
    s = s.strip().upper()
    allowed = set('ATCG')
    for i, ch in enumerate(s):
        if ch not in allowed:
            return i, ch
    return -1, None

Troubleshooting notes: always use a safe input method (fgets in C), strip CR/LF, and check for truncation if the buffer fills. Cast to unsigned char before calling ctype functions. Decide whether to accept lowercase (examples convert to uppercase), and whether to treat U as valid (RNA). For very large sequences a 256-entry boolean lookup table is slightly faster than repeated comparisons. If you only need a yes/no answer and prefer regex or library helpers, those are fine — the loop gives the best error diagnostics.

Recommended Answers

All 3 Replies

Something simple like a for loop to check each character in turn perhaps?

Or you could just use the strspn() function: if (strspn( dnaStr, "AaTtCcGg" ) < strlen( dnaStr )) puts( "error in DNA" );

commented: Aye +4

Or you could just use the strspn() function: if (strspn( dnaStr, "AaTtCcGg" ) < strlen( dnaStr )) puts( "error in DNA" );

@BensonRoss, make sure you include <string.h> for strspn() and strlen().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.