### need to find frequency of letters

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

 ``1234567891011121314151617181920212223242526`` `````` 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++; } } } }``````
 ``123`` `````` 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 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?

 ``123456`` ``````for(...) dnaCopy = dna[I]; for(...){ //... } }``````

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

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