Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an empty string contain an empty string in C++?

Just had an interesting argument in the comment to one of my questions. My opponent claims that the statement "" does not contain "" is wrong.

My reasoning is that if "" contained another "", that one would also contain "" and so on.

Who is wrong?

P.S.

I am talking about a std::string

P.S. P.S

I was not talking about substrings, but even if I add to my question " as a substring", it still makes no sense. An empty substring is nonsense. If you allow empty substrings to be contained in strings, that means you have an infinity of empty substrings. What is the point of that?

Edit:

Am I the only one that thinks there's something wrong with the function std::string::find?

C++ reference clearly says

Return Value: The position of the first character of the first match.

Ok, let's assume it makes sense for a minute and run this code:

string empty1 = "";
string empty2 = "";

int postition = empty1.find(empty2);

cout << "found \"\" at index " << position << endl;

The output is: found "" at index 0

Nonsense part: how can there be index 0 in a string of length 0? It is nonsense.

To be able to even have a 0th position, the string must be at least 1 character long.

And C++ is giving a exception in this case, which proves my point:

cout << empty2.at( empty1.find(empty2) ) << endl;

If it really contained an empty string it would had no problem printing it out.

like image 540
Oleksiy Avatar asked Aug 01 '13 14:08

Oleksiy


People also ask

Does string contain empty string?

An empty string occurs in every string. Specifically, a contiguous subset of the string must match the empty string. Any empty subset is contiguous and any string has an empty string as such a subset. Returns true if and only if this string contains the specified sequence of char values.

Does empty language contain empty string?

The empty string should not be confused with the empty language ∅, which is a formal language (i.e. a set of strings) that contains no strings, not even the empty string. The empty string has several properties: |ε| = 0. Its string length is zero.

Can you have an empty string in C?

In C language strings are terminated with a null character \0 , so we can check if a given string is empty or not by verifying if a string contains the first character is \0 . Note: C language doesn't have any built-in function to check if a string is empty, like we have a empty() function in C++.

Is empty string considered null in C?

They are different. A NULL string does not have any value it is an empty char array, that has not been assigned any value. An empty string has one element , the null character, '\0'. That's still a character, and the string has a length of zero, but it's not the same as a null string, which has no characters at all.


2 Answers

It depends on what you mean by "contains".

The empty string is a substring of the empty string, and so is contained in that sense.

On the other hand, if you consider a string as a collection of characters, the empty string can't contain the empty string, because its elements are characters, not strings.

Relating to sets, the set

{2}

is a subset of the set

A = {1, 2, 3}

but {2} is not a member of A - all A's members are numbers, not sets.

In the same way, {} is a subset of {}, but {} is not an element in {} (it can't be because it's empty).

So you're both right.

like image 187
molbdnilo Avatar answered Sep 18 '22 18:09

molbdnilo


C++ agrees with your "opponent":

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

int main()
{
    bool contains = string("").find(string("")) != string::npos;
    cout << "\"\" contains \"\": "
        << boolalpha << contains;
}

Output: "" contains "": true

Demo

like image 22
jrok Avatar answered Sep 17 '22 18:09

jrok