2

I just noticed this strange behavior of string::find. I have a non-empty string b and another empty string a. When I call b.find(a) it should return npos but returning 0.

#include <iostream>
#include <string>
using namespace std;

int main() {
    // your code goes here
    string a , b("ABC");
    if ( string::npos == b.find(a) ) std::cout << std::endl << "TRUE" << std::endl;
    return 0;
}

Above code doesn't print true. Can someone please explain me what this means ? Since a is empty and b is non-empty finding a empty string in non-empty doesn't make sense and hence error. So it should return npos

Thanks

9
  • 7
    The empty string is indeed contained in any other string, so the result 0 is correct Commented Dec 1, 2015 at 11:49
  • 3
    "When I call b.find(a) it should return npos" - where do you get that idea from? In future, check cppreference docs before presuming such things - it explains it very clearly. Commented Dec 1, 2015 at 11:51
  • 1
    If you read e.g. this find reference you will see all conditions that needs to be true for the sub-string to be found, and if you carefully check them you will see that the empty string indeed causes all to be true. Commented Dec 1, 2015 at 11:51
  • 1
    @noname It is not non-empty string. It is contained in it, as everyone is telling you. Commented Dec 1, 2015 at 11:53
  • 2
    "how come empty string can be non-empty string?" - are you misunderstanding what b.find(a) does? It searches for a in b, not the other way around.... Commented Dec 1, 2015 at 11:54

1 Answer 1

11

Empty string is a substring of all strings. The first position where an empty substring exists is the first index. find returns the first index where it finds the first occurrence of the substring.

If the definition of empty substring confuses you, consider the algorithm that checks if string is a substring. The algorithm checks each character in the potential substring and compares it to the corresponding character in the other string. If any character does not match, then it is not a substring. If the end of the searched string is reached, then it is a match. In the case of an empty string, no character can differ because there are no characters. The end is reached immediately and the conclusion is that the empty string is a substring.

Sign up to request clarification or add additional context in comments.

2 Comments

"In fact, all strings contain infinitely many empty substrings." - that's a bit OOT - I'd count them as one per position in the string, though it's true that naive find-at-pos / pos += x.size() logic could get stuck in an infinite loop....
@TonyD whether either of our claims is correct seems very much a philosophical question. I'll remove it since it's not essential.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.