string help :(

Pages: 12
closed account (Djvk92yv)
.

Last edited on
Didnt you already ask this exact question yesterday? You got answers there.
closed account (Djvk92yv)
.
Last edited on
string is part of the std::, so is cout and cin So you are using it all the time. std is just the standard library in c++ and you use it all the time.
Last edited on
closed account (Djvk92yv)
.
Last edited on
You could do something like this -


1
2
3
4
5
6
char seq[12] = { 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G' };
std::string protein[4] = { "ATC", "GAT", "CGA", "TCG" };
	
protein[0].assign(seq, 3);

std::cout << protein[0] << std::endl;


You see this - protein[0].assign(seq, 3); The number 3 is how many characters from the char array you want to put in the protein[0]. So if you change from number 3 to say, 5. and print out protein[0] You will get
ATCG
closed account (Djvk92yv)
.
Last edited on
I spefically told you.

The number 3 is how many characters from the char array you want to put in the protein[0]. So if you change from number 3 to say, 5. and print out protein[0] You will get
ATCG


If you want everything to come out, you change the number 3, to number 12.

protein[0].assign(seq, 12);
closed account (Djvk92yv)
.
Last edited on
You never said that. Now you're saying you just want to print out the string array?

1
2
3
4
for (int i = 0; i < 4; i++)
{
	std::cout << protein[i] << " ";
}
closed account (Djvk92yv)
.
Last edited on
This -

1
2
3
4
for (int i = 0; i < 4; i++)
{
	std::cout << protein[i] << " ";
}


Does NOT only show ATC. It shows everything.
ATC GAT CGA TCG


I wont further respond to this thread unless you have a qustion that I can understand.
closed account (Djvk92yv)
.
Last edited on
Now all of a sudden your string array is empty?..................

Im gonna quote myself quoting myself here -


I spefically told you.

The number 3 is how many characters from the char array you want to put in the protein[0]. So if you change from number 3 to say, 5. and print out protein[0] You will get
ATCG


If you want everything to come out, you change the number 3, to number 12.

protein[0].assign(seq, 12);


Change the 3, to 12.

And you dont need a string array, you only need one string.

1
2
3
4
5
6
7
char seq[12] = { 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G' };
string protein;

protein.assign(seq, 12);

std::cout << protein;
	
Last edited on
closed account (SECMoG1T)
i would use a vector of strings :D


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
27
28
29
30
31
32
33
34
35
36
37
38
39

template<std::size_t N>
std::vector<std::string> generate_sequences(char(&array)[N])
 {
      std::vector<std::string> seq;
      std::size_t pos=0;

      while(pos<N)
       {
           seq.push_back(std::string(array,pos,3));
           pos+=3;
       }

 return seq;
 }
      
void print(std::vector<std::string>& vec)
{
    for(const auto& seq : vec)
         std::cout<<seq<<" ";
         std::cout<<"\n\n";
}

int main()
 {
     char seq[12] = { 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G' };///for input provided total  
                                                                                        ///bases value is divisible by three

  auto sequences=generate_sequences(seq);
  print(sequences);

  char seqptr[]="ATCGCCACTAAACGCATGGCACCCGAATCAGGGCGTCATTTAGCTTGACGAGCCCGGATACCGGAGTGCCGAAGT";
  auto seq2=generate_sequences(seqptr);

  print(seq2);
 
 return 0;
 }
       
Last edited on
closed account (Djvk92yv)
.
Last edited on
Yeh @Andy... Lets not get too advanced here :)
closed account (SECMoG1T)
@TarikNeaj the whole idea is the same only that i added a function with a template to help deduce the array size then pushes three bases into a vector as a string object, thats all
Which is not maybe advanced in and of itself, but relatively to this guy its advanced, you would know that if you read the thread.
Last edited on
closed account (SECMoG1T)
Ooh yeh, i probably assumed everything but maybe we can simplify that to this judt to make sure the op understands

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void generate_sequences(char arr[],int size,std::string result[])///result is an array large enough to hold all resulting strings
 {
      int pos=0,counter=0;
       while(pos<size)
       {
           std::string temp_seq(arr,pos,3);//this string constructor copies three characters from position pos in arr
           result[counter]=temp_seq;
           ++counter; pos+=3;///just increment pos by three each time
       }
 }

 void print(std::string arr[],int size)
{
   for(int i=0;i<size;i++)
   {
       std::cout<<arr[i]<<" ";
   }
}

int main()
 {
     char seq[12] = { 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G' };///for input provided total
                                                                                   ///bases value is divisible by three
     const int sz=12/3;
     std::string result[sz];

     generate_sequences(seq,12,result);
     print(result,sz);

     char seqptr[90]{'A','T','C','G','C','C','A','C','T','A','A','A','C','G','C','A','T',
                     'G','G','C','A','C','C','C','G','A','A','T','C','A','G','G','G','C',
                     'G','T','C','A','T','T','T','A','G','C','T','T','G','A','C','G','A',
                     'G','C','C','C','G','G','A','T','A','C','C','G','G','A','G','T','G',
                     'C','C','G','A','A','G','T','T','A','G','T','C','T','T','A','G','A',
                     'A','T','G','G','T'
                    };

     std::string result2[30];

     generate_sequences(seqptr,90,result2);

     print(result2,30);

 return 0;
 }
Pages: 12