lundi 30 janvier 2017

One char from the other bool function

I want to have a function that is going to return true if and only if a char* s could be obtained from char* t by simply crossing out certain letters e.g. g("ERT", "EAARYT")=true and g("ERT","ABCT")=false.

My idea for this code is the following:

bool g(char* s, char* t) {
    for (int i=0; s[i]!=0;i++) {
        for (int j=i; t[j]!=0; j++) {
            if (s[i]==t[j]) {
                return true;
            }
        }
    }
    return false;
}

Obviously it does not work as it only check whether the first letter is present and then immediately returns true. How should i change that?

I would prefer to use nested loops/if constructions this should be more than doable with that.

Aucun commentaire:

Enregistrer un commentaire