need to find frequency of letters

i need to find the frequency of letters. here is my code what am i doing wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  void QubiEngine::nucl_frequency()
{
     int freqA=0;
     int freqT=0;
     int freqC=0;
     int freqG=0;
     string dnaCopy ;
      for(int i = 0; i < dna.size(); i++){
              dnaCopy = dna[i];
         for (string::iterator it = dnaCopy.begin(), endOfString = dnaCopy .end(); it != endOfString; it++){
         if (*it == 'a'){ 
            freqA++; 
            }
            if(*it == 't'){
               freqT++;
              }
            if (*it == 'c'){
               freqC++;
            }
            if (*it == 'g'){
               freqG++;   
            }
            }
                               
       }
}
1
2
3
     string dnaCopy ;
      for(int i = 0; i < dna.size(); i++){
              dnaCopy = dna[i];


This is a bit dubious.
If the variable dna is itself an std::string, you could just do this instead of writing a for() loop:

string dnaCopy = dna;

Even if dna isn't an std::string, but a container of char such as std::vector<char> or std::list<char> you could do:

string dnaCopy(dna.begin(), dna.end()); // use range constructor
http://cplusplus.com/reference/string/string/string/

The rest of your code looks fine... so please describe your problem in more detail.
the dna is a string in vector, but how do i cout the number frequency if its void

i need to have A=some int
and so on
never mind i just answered my own question since its frequency i need a percent i did simple math already.



thanks for the help anyways
The copy doesn't seem necessary at all. Why not get rid of assigning a string with one char at a time and instead iterate through the original dna string directly?

Instead of:
1
2
3
4
5
6
for(...)
   dnaCopy = dna[I];
   for(...){
     //...
   }
}


Condense it to:
1
2
3
4
for(int I = 0; I < dna.size(); I++){
   if(dna[I] == 'a')
//.....
}


Or better yet:
1
2
3
4
5
6
for(int I = 0; I < dna.size(); I++){
   switch(dna[I]){
      case 'a':
         //...
   }
}
Topic archived. No new replies allowed.