Question: So I'm passing an string into this function, where the length of the array is at a default of 50. 

I input the string using this 

```
void inputDNA(char *dnaSequence)
{
    printf("Enter the sequence.");
    scanf("%s", dnaSequence);
}

```

So I figured /n would be in the last position of the array if its less than 50. So I have this for loop...

```
for(i = 0; i<length || dnaSequence[i] == '\n'; i++)
    {
        switch(dnaSequence[i])
        {
            case 'A': aCount++;
            break;
            case 'a': aCount++;
            break; //do this for g,c,t but for readability I removed them for this post.

            default: 
            {
                printf("Invalid Base %c\n", dnaSequence[i]);
                break;
            }
        }
    }

```

To read the string and count all a,g,t,c but print out any invalid bases. It does this till it finds the /n or it reaches the end of the array. It does it correctly for the string I entered, but then it just throws in random characters from the rest of the array for the last half and calls them all invalid if they are. 

So just to illustrate what I'm talking about, here it is in action. [Image](http://i.imgur.com/cYXDYjy.png)

Any help?

Answer: 
The memory reserved for `dnaSequence` is dirty. `scanf` puts the read characters at the beginning but the end stays the same. You should check for the end of the c-string in the `for` loop.

```
for(i = 0; i<length && dnaSequence[i] != '\n' && dnaSequence[i] != '\0'; i++)

```

EDIT: Tt worth to mention that `scanf` does not read the end-of-line character `\n`. So checking for it is unnecessary.

```
for(i = 0; i < length && dnaSequence[i] != '\0'; i++)

```
Your logic in your for statement is bad. Your for will end when the conditional part of your for loop returns false.

Remember that for's syntax is:
 for(initialization; conditional; increment)

Your conditional should be using an And, not an Or, since i

Try

```
for(i=0; i<length && dnaSequence[i] !='\0'; i++)

```