Substrings in c ++

0

You see, I have to do a pseudo code test tomorrow and now I'm getting a bit involved with the theme of strings ... Reviewing I was doing an exercise that asked us to do a program that said if the first string was substring of the second one, that is, if it appears or not. The case is that I know how to do it with a for and when it is fulfilled "X" condition, I put a break, and fixed. But the teacher says that if there's a break in a for, that for is possibly a while, and that's what I tried to do, but I do not get, this is the last code I've tried.

int main()
{

    string cadena1, cadena2;
    int comptador = 0, longitudCadena = 0, longitudCadena2, pos1 = 0, pos2 = 0;
    bool substr = false;


    cout << "Entra la cadena" << endl;
    getline(cin, cadena1);
    cout << "Entra la cadena 2" << endl;
    getline(cin, cadena2);

    longitudCadena = cadena1.length() - 1;
    longitudCadena2 = cadena2.length() - 1;


    while (pos2 < cadena2.length()){

        if (cadena1[pos1] == cadena2[pos2]){
            pos1++;
        }else{
            pos1 = 0;
        }

        if (pos1 == longitudCadena){
            substr = true;
        }else{
            substr = false;
        }

        pos2++;

    }


    if (substr == true){
        cout << "Es substring" << endl;
    }else{
        cout << "No es substring" << endl;
    }



    return 0;
}
    
asked by THR4SH3RP0L0 01.02.2017 в 12:35
source

1 answer

2

for and while have two basic differences:

  • for allows the initialization of variables in the declaration
  • for has a special site to make the increments between iteration

Because of these two differences, choosing between for and while should not be measured in whether the loop is going to have a break but in the particularities of each loop. An example to search for an item in a list:

// condiciones iniciales
int numeros[100];
int indice = -1;

// for v1
for( int i=0; i<100; i++ )
{
  if( numeros[i] == 25 )
  {
    indice = i;
    break;
  }
}

// for v2
for( int i=0; i<100 && indice == -1; i++ )
{
  if( numeros[i] == 25 )
    indice = i;
}

// while
int i = 0;
while( indice != -1 )
{
  if( numeros[i] == 25 )
    indice = i;
  else
    i++;
}

I do not know about you, but I prefer, in this case, one of the first two options even if only for clarity.

From my point of view, for should be used to iterate over ranges whose limits are known and leave while for those cases where the iteration range is rather diffuse ...

That said, your algorithm for locating a substring in a string is incorrect:

while (pos2 < cadena2.length()){

    if (cadena1[pos1] == cadena2[pos2]){
        pos1++;
    }else{
        pos1 = 0;
    }

    if (pos1 == longitudCadena){
        substr = true;
    }else{
        substr = false;
    }

    pos2++;
}

The search should include two loops and you only have one. You should check the substring for each character in the main chain, which implies a loop to iterate over the main chain and another to know if the substring is from that position.

for( int i=0; cadena1[i] != 0; i++ )
{
  for( int pos1 = i, pos2 = 0; ;pos1++, pos2++  )
  {
    if( cadena2[pos2] == 0 )
    {
      substr = true;
      break;
    }
    if( cadena1[pos1] != cadena2[pos2] )
      break;
  }

  if( substr )
    break;
}

Or if you prefer without break :

for( int i=0; cadena1[i] != 0 && !substr; i++ )
{
  for( int pos1 = i, pos2 = 0;                       // inicializadores
       !substr && cadena1[pos1] == cadena2[pos2];    // comparaciones
       pos1++, pos2++, substr = (cadena2[pos2] == 0))// incrementos
  { }
}

And if you prefer it with a while :

for( int i=0; cadena1[i] != 0 && !substr; i++ )
{
  int pos1 = i, pos2 = 0;
  while( !substr )
  {
    if( cadena1[pos1] != cadena2[pos2] )
      break;

    pos1++, pos2++;
    substr = (cadena2[pos2] == 0);
  }
}
    
answered by 01.02.2017 / 12:59
source