16
I’m trying to do that function with the help of the functions strcmp()
and strncmp()
and I’m not having much success.
Let’s say I have the string char s[] = "abc"
and another string char v[] = "cdeabf"
, the output would give 0 (true value) because the characters [a
, b
and c
] belong to v
. Otherwise it would give -1 (false value).
If there is a predefined function that helps me, what would it be? And if you could do this function without the help of predefined functions, how would you do it?
Do you know the size of the strings? I gave an answer that uses no predefined function except for for
strlen
(otherwise there would be no way of knowing how long the strings are). But if you already know these values beforehand, then you don’t need to...– mgibsonbr
@mgibsonbr although I have understood the intention of the comment have to say that it is possible to discover the size of the string without a predefined function, just write a code that does exactly what the
strlen
does :P Something like that:const char *s; for (s = suaString; *s; ++s); int tamanho = s - suaString;
. Just out of curiosity.– Maniero
@bigown haha of course, my fault! What a
strlen
does is precisely count the characters up to - but not including - the null terminator, make it easy to do by hand if necessary... : P– mgibsonbr