-1

Task:Write a code to the new string of Dna According to its pattern. Just so you know In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". Fore example:DNA_strand ("ATTGC") //returns "TAACG" or DNA_strand ("GTAT") //returns "CATA" My Code=>

#include <string>
#include <vector>
#include <iostream>
std::string DNAStrand(const std::string& dna)
{
  std::string Sym;
  std::string c;
  std::stringstream s;
  s << Sym;
  s >> c;
  for(int i = 0; i < dna.size() - 1; i++) {
      switch (dna[i]) {
          case ('A'):
          Sym[i] = 'T';
          break;
          case ('T'): 
          Sym[i] = 'A';
          break;
          case ('C'):
          Sym[i] = 'G';
          break;
          case ('G'):
          Sym[i] = 'C';
          break;
          default:
          std::cout << "invalid";
} return c.str();
}

int main() {
  std::cout << DNAStrand("ATTGC") << "\n"; //retun "TAACG"
  std::cout << DNAStrand("GTAT") << "\n";  //retun "CATA"
}
}
  • What is your question? What does this code do when run? Why is that wrong? Show example input, desired output, and current output; explain problems in full; quote error messages in full. – underscore_d Dec 11 '20 at 09:24

1 Answers1

0

You have created a vector<string>, but in the if statements, you are setting the elements of the vector to chars. You want to build a string, not a vector<string>.

You should replace subsequent if statements with else if, or use a switch statement. Otherwise if statements subsequent to a satisfied if statement are executed needlessly.

Replace this vector with an ostringstream. Naming the stream as s, you would append a char named c with s << c. At the end of iterating over dna, return s.str().

justinpc
  • 797
  • 6
  • 19
  • how should I replace the vector with an ostringstream??I`m confused a little – Poorya Keshavarzi Jan 06 '21 at 09:35
  • @PooryaKeshavarzi You are setting characters as the elements of a vector of strings. Get rid of the vector, and push the characters to an ostringstream instead. Then get the result by calling .str() on the ostringstream. – justinpc Jan 06 '21 at 10:48
  • I updated my question.Tell me what to change – Poorya Keshavarzi Jan 06 '21 at 10:56
  • Read the documentation for string and ostringstream, if you want to use that. If you want to pre-allocate the string, then you need to initialise it with the right size. – justinpc Jan 06 '21 at 11:14