简体   繁体   中英

How do you check if content of string starts with the content of another string?

I'm trying to figure out if there is a way to compare if a content of a string is the start of another string. For example, I want to know the number of strings that start with the string "c", in an array whose content's is [cowboy, air, cow, cat]. Using the compare function from the string library works fine. The issue is when instead of trying with "c", I try with "b" I get the same number of answers. I don't know the reason why, Does anybody have a suggestion on how to fix the problem? Here are the two versions that I have.

 #include <iostream>
 #include <string>
using namespace std;
 int main() {
// insert code here...
string A[4] = {"cowboy", "air", "c", "count"};
string b = "c";
int count = 0;

for(int i = 0; i < 4; i++)
{
    if(b.compare(A[i]) == 0 || b.compare(A[i]) == -1)
        count++;
}
cout << count << endl;
}

The output for this part is 3, which is right

 #include <iostream>
 #include <string>
using namespace std;
 int main() {
// insert code here...
string A[4] = {"cowboy", "air", "c", "count"};
string b = "b";
int count = 0;

for(int i = 0; i < 4; i++)
{
    if(b.compare(A[i]) == 0 || b.compare(A[i]) == -1)
        count++;
}
cout << count << endl;
}

The output for this part is also 3, which is wrong.

Any help would be really appreciated!

You can try using the find function:

string A[4] = {"cowboy", "air", "c", "count"};
string b = "b";
int count = 0;

for(int i = 0; i < 4; i++)
{
    auto found = A[i].find(b);
    if(found != string::npos && found == 0)
        count++;
}
cout << count << endl;

What I do here is find b in A[i] , if I get npos which means it wasn't found if I do find it, I check if its at the start by checking found == 0

Similarly to simplify if I only want to check for just a character match at the start I could simply A[i] == b[0]

Live Demo

There is an overload of the compare function which accepts position and a length, and compares only a part of the string to the other string. If you pass 0 for the position, and the size of the string you are searching for as the length, it will only compare that many characters at the start of the string, instead of comparing the whole thing.

if(A[i].compare(0, b.size(), b) == 0)
    count++;

By the way, the only reason your first test appeared to be working, was that you were basically checking if "c" is lexicographically less than or equal to your target strings. And since that is the case for "cowboy", "c" and "count", but not "air", your result was 3. But if you added a string like, "direwolf", which comes lexicographically after "c", but does not start with "c", you would find that your results were not what you are expecting.

Replace your if condition with the following. This compares the string b with A[i]'s 1st character.

if(b.compare(0, A[i].length(), A[i], 0,  1) == 0)

In your for loop when you pass in b = "c" the first conditions satisfies b.compare(A[i]) == 0 and for b="a" second condition satisfies. So in both cases you see 3.

您可以通过将字符串b更改为字符b,然后检查数组中每个字符串的第一个字母是否等于b并在它们相等时增加计数来轻松实现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM