-4

If the user enters any character other than ACGT (sequence does not matter) then it should print "INVALID". The user just keeps entering the sequence not more than 250 characters.

#include <stdio.h>
#include <string.h>
int main(void)
{
    char dna[250];
int i;
for(i=0; i<250; i++)
{
    scanf("%c", dna[i]);
}
fgets(*dna, 250, scanf("%c", dna));
int k;
for(k=0; k<250; k++)
{
    if(dna[k] == 'A' || 'C' || 'G'|| 'T')
    {
        i++;
        //printf("%c\n", dna[0])
    }
}
if(i > 0)
{
    printf("VALID sequence \n");
}
else
{
    printf("INVALID sequence \n");
}

}
Nabeel
  • 1
  • 2
  • 4
    This is a Q&A site. You didn't ask a question. – StoryTeller - Unslander Monica Mar 07 '17 at 21:05
  • 2
    Please [pick up a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read more about the logical AND (`&&`) and OR (`||`) operators and how they work. – Some programmer dude Mar 07 '17 at 21:06
  • 2
    And read [a `fgets` reference](http://en.cppreference.com/w/c/io/fgets) or tutorial too. Together with [a `scanf` (and family) reference](http://en.cppreference.com/w/c/io/fscanf) or tutorial as well. Your program, as shown to us, should make the compiler *scream* at you. – Some programmer dude Mar 07 '17 at 21:07
  • And last but not least, read [ask]. – dandan78 Mar 07 '17 at 21:07
  • 1
    `if(dna[k] == 'A' || 'C' || 'G'|| 'T')`-> `if (dna[k] == 'A' || dna[k] == 'C' || dna[k] == 'G'|| dna[k] == 'T')`. Read your C textbook. And `fgets(*dna, 250, scanf("%c", dna));` is total nonsense, what are you trying to achieve? You probably want `fgets(dna, 250, stdin);`. – Jabberwocky Mar 07 '17 at 21:09
  • I am in process of learning C, and I do not have any prior experience with this language. ALSO i am using this website for the first time, if you cant look that up, then i dont think you should be roasting/judging my coding skills lol. PEACE! – Nabeel Mar 09 '17 at 01:48

2 Answers2

1

like this

#include <stdio.h>

int main(void){
    char dna[250+1], rest;
    if(scanf("%250[ACGT]%c", dna, &rest) == 2 && rest == '\n')
        printf("VALID sequence \n");
    else
        printf("INVALID sequence \n");
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Can you please explain the scanf and the whole if statement senpai! @bluepixy – Nabeel Mar 09 '17 at 00:09
  • [scanf](http://en.cppreference.com/w/c/io/fscanf). Read strings consisting of `ACGT` with a limit of 250 characters When the reading is successful. If there are unacceptable characters or exceeds the size `rest` is not a newline. – BLUEPIXY Mar 09 '17 at 00:22
0

There are a large number of ways to approach the problem. You can use scanf with character classes as shown, or you can use any other method to read the input (e.g. getchar, fgets, POSIX getline) and then simply analyze the characters entered for anything other than "ACGT".

Additionally, in your problem statement you state "The user just keeps entering the sequence not more than 250 characters". Obviously you will need to loop to handle entry of multiple strings, but beyond that, you will also need to protect against and handle the remainder of any strings greater than 250 characters. It is unclear whether in that case, you want to keep the first 250 valid characters entered (seems logical you would), and then discard any over the 250 character limit.

Your tools for validating the input are, in the case of using character-oriented input (e.g. using getchar) are simply to check each character input against your ACGT. When using line-oriented input, (e.g. fgets or getline) the C library provides a number of tools to check for characters within a string, or substrings within a string (strchr is applicable here) or you can simply walk a pointer down the entire string of input checking each character. (you also need to check for, and remove the '\n' the line-oriented functions read and include in the buffer)

Putting the pieces together into a short example using fgets for input and strchr to check whether each character is one of ACGT, you could do something like the following. Here the user can enter as many strings as desired. The program terminates when EOF is read (manually generated with Ctrl + D on Linux, Ctrl + Z on windoze). In the event an invalid string is entered, the code identifies the position of the first invalid character in the entry:

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

#define MAXC 250

int main (void) {

    char str[MAXC+1] = "", *valid = "ACGT";

    printf ("Enter sequences [ctrl+d] to quit:\n");
    while (fgets (str, MAXC+1, stdin))          /* read input */
    {
        size_t len = strlen (str), good = 1;    /* get length, set flag */
        char *p = str;                          /* pointer to str */
        int c;

        if (str[len-1] == '\n')                 /* trim '\n' char */
            str[--len] = 0;                     /* overwrite with nul */
        else                                    /* line > 250, discard extra */
            while ((c = getchar()) != '\n' && c != EOF) {}

        for (; *p; p++)                         /* for each char in str */
            if (!strchr (valid, *p)) {          /* check against valid  */
                good = 0;                       /* not found - invalid */
                break;
            }

        if (good)
            printf ("VALID\n");
        else
            fprintf (stderr, "INVALID ('%c' at character '%ld'\n", *p, p - str);
    }

    return 0;
}

Example Use/Output

$ ./bin/acgtvalid
Enter sequences [ctrl+d] to quit:
ACGTTTGGCCCATTAGGC
VALID
ACCGGTTCCGGAITT
INVALID ('I' at character '12')
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85