C - error after for () cicle

I encoded in C to solve the problem on the ROSALIND website. The code is very simple, and since it is so simple, it is very difficult to fix this error (I think) Here is my code:

    /* A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.

An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."

Given: A DNA string s of length at most 1000 nt.

Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in s. */

#include <stdio.h>

int main(){
    char nt;
    int nA, nC, nG, nT;
    for(int i = 0, nA = nC = nG = nT = 0; i < 1000; i++){
        nt = getchar();
        if(nt == 'A'){
            nA++;
        }else if(nt == 'C'){
            nC++;
        }else if(nt == 'G'){
            nG++;
        }else if(nt == 'T'){
            nT++;
        }else{
            break;
        }
    }
    printf(" %d %d %d %d", nA, nC, nG, nT);
}

And when I check this code:

AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC

He must give:

20 12 17 21

But my computer gives:

4200624 12 17 21

I found printf () functions to find the location of the error. I saw this at that moment, right before I exit the cycle nA = 20, but the moment immediately after it was nA = 4200624. What can I do?

+4
source share
2 answers

, , for, 0. nA i, , . for, . - . , . , , .

+10

. "nA", , - .

+2

Source: https://habr.com/ru/post/1627703/


All Articles