Why is it evaluating to true? Shouldn't ACGT be skipped?
Printable View
Why is it evaluating to true? Shouldn't ACGT be skipped?
When sequence[0] is 'A' on the first time through the loop, this fails:Quote:
Code:for(x = 0 ; x < strlen(sequence) ; x++)
{
while( sequence[x] )
{
if( sequence[x] == 'U' )
{
strcpy(out_field, "RNA") ;
return ;
}
else if (sequence[x] != 'A' || sequence[x] != 'C' || sequence[x] != 'G' || sequence[x] != 'T' || sequence[x] != EOF || sequence[x] != '\n' )
{
strcpy(out_field, "PROTEIN") ;
return ;
}
else
{
strcpy(out_field, "DNA" ) ;
return ;
}
}
}
So then this multi-condition is evaluated:Code:if( sequence[x] == 'U' )
Is sequence[0] != 'A'? No, (is it equal to 'A'), so this first condition is false, therefore the next condtion is evaluated...Code:else if (sequence[x] != 'A' || sequence[x] != 'C' || sequence[x] != 'G' || sequence[x] != 'T' || sequence[x] != EOF || sequence[x] != '\n' )
Since sequence[0] is not equal to 'C' (it IS equal to 'A'), then this condition is now true (since you said "OR"), thus, "PROTEIN" is returned.Code:if (sequence[x] != 'C' ) ...
Todd
You can do that if you make the return type const char*. However, that also means you can't modify your "DNA" string. Otherwise, you would be returning a pointer to a variable that is local to the function. According to scope rules, the local variable would be destroyed when the function returns, thus the function receiving the data would get a pointer to invalid data (garbage).
So to provide a modifiable string, you need to copy it into a proper buffer. And that's why you pass in an argument. But in doing so, there's no need for any return value, thus it's set to void.
Hmmmm...do you think this is homework? If it is, I totally agree with you. A bioinformatics course maybe? If so, shouldn't the OP have had *some* background in programming before being accepted into such a course?
In any event my totally rewritten solution has been even more totally ignored, so I'm not overly concerned. I could argue that it at least shows the correct logic, which seems to be somewhat of a sticking point here.