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;
}