Jagoda
Jagoda

Reputation: 424

Substring into array

I have an issue. I would like to insert the result of substr function into the array, but it reads my string not from the beggining to the end but it omits the big part of it and starts to read at the end. And it doesn't put the reult in the array correctly - every array cell contains the same substring I'm using structures. Here is the part of my code, the input looks like that:

TTGATTCTATGGAGGGATGCTGGCAAGGCTCCGGAAGCAGCATCAGCAATTAAAAAATTACTGGACCTGATCTT

And the code

struct sekwencja

    struct sekwencja
{
  string sekwencja;
  string etykieta[10000];
  int jakosc[200];
  int dlugosc;
  string idf;
  string idq;
}sek[100];

(...)

sek[i].dlugosc=sek[i].sekwencja.length();

(...)

cout<<"give the length of substring"<<endl;
cin>>h;

for (int i=0;i<7;i++)
{
    for (int a=1; a<sek[i].dlugosc-1; a++)
    {
      if(sek[i].sekwencja.substr(a,h).length()==h){
          for(int b=0;b<sek[i].dlugosc-1+h;b++)
        {
            sek[i].etykieta[b]=sek[i].sekwencja.substr(a,h);
          }
          }
}
}

I would be very grateful for your help!

EDIT:

I don't know why I can do something like this:

int b=0;
string etykieta [1000];
for (int i=0;i<7;i++)
{
    for(int a=0;a<sek[i].dlugosc;a++)
    {
            etykieta[b]=sek[i].sekwencja.substr(a,h);
            b++;
}
}

but not like this

int b=0;
string etykieta [1000];
for (int i=0;i<7;i++)
{
    for(int a=0;a<sek[i].dlugosc;a++)
    {
            sek[i].etykieta[b]=sek[i].sekwencja.substr(a,h);
            b++;
}
}

Upvotes: 0

Views: 178

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153802

First off, note that creating a substring actually, well, creates a substring which consists of a copy of the original characters. If you want to now if s.substr(a, h).size() == h you are much better off to test if s.size() <= a + h: this expression doesn't create a substring but merely compares a couple of integers.

You unfortunately omit the declaration and types of the entities you have and I have no comprehension at all of the names you are using. Note, however, that the inner loop only varies b and neither touch a or i, i.e., the expression

sek[i].sekencja.substr(a, h)

will always be the same (looking at that: is that Romanian for "sequence"?). Why you only see the tail of you input, I don't know.

Upvotes: 1

Related Questions