-5

my question is in c++.how to compare two different length string. for example string a = "ap" string b = "apple". so the final match should be 1, or consider another example, let's say string a = "123" string b = "123123123", the final match should be 3. so what i'm think is i try to let a[i]= b[i] but it's just comparing only 1 charcater. how to compare a muliple length string.

int getMatchCount(string a, string b)
{
    int match = 0;

    if (a.length() == 0 or b.length() == 0 )
    {
        return -1;
    }

    for (int i = 0; i < a.length(); i++)
    {

        if( string.compare(a, b) )
        {
            match = match +1;
        }
    }
    return match;
}
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
bro
  • 21
  • 3
  • Take a look at http://en.cppreference.com/w/cpp/string/basic_string/find and maybe http://en.cppreference.com/w/cpp/string/basic_string/substr – rbf Feb 15 '18 at 05:56
  • You may want to read about [regular expressions in c++](http://en.cppreference.com/w/cpp/regex). But beforehand you may want to take the [tour] again, learn [ask] and then come back and ask a new question. – muXXmit2X Feb 15 '18 at 05:59
  • What sould be a = 'abc', b = '123 abc 456' a = 'abc', b = 'abc something else abc again' ? Its not clear what you want to do... – Wolfgang Feb 15 '18 at 07:20
  • `string.compare(a, b)` is not C++. – molbdnilo Feb 15 '18 at 09:37

2 Answers2

3

Here is a naive, but viable solution:

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, const char * argv[]) {
    std::string first = "hello";
    std::string second = "hello_world";
    int match = 0;
    for (int i = 0; i < std::min(first.length(), second.length()); ++i) {
        if(first.at(i) == second.at(i)) {
            match++;
        } else {
            break;
        }
    }
    std::cout << match << std::endl;
    return 0;
}

Because it was requested:

The #include <algorithm> was there for the std::min() function in order to avoid accessing an invalid memory address. That is, it prevents the for loop from accessing any string index greater than string.length(), which would result in undefined behaviour ...or back when I was learning C++, a segmentation fault.

In this case it has been replaced with some logic and the short-hand ternary operator.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    string first = "hello";
    string second = "hello_world";
    int match = 0;
    size_t lowest = first.length() <= second.length() ? first.length() : second.length();
    for (int i = 0; i < lowest; ++i) {
        if(first.at(i) == second.at(i)) {
            match++;
        } else {
            break;
        }
    }
    cout << match << endl;
    return 0;
}
pcgben
  • 726
  • 7
  • 24
  • That's not what the OP wants, it will compair character by character. – muXXmit2X Feb 15 '18 at 06:01
  • If the op adds an inner loop the problem is solved, the solution shows a simple, intuitive way to solve the problem. – Wolfgang Feb 15 '18 at 06:21
  • sorry I haven't learnt #include, can u explain my question with a more simple way, and I hope u can delete those std:: and instead using namespace std; ty – bro Feb 15 '18 at 06:26
  • @bro - namespace https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Wolfgang Feb 15 '18 at 07:24
0

Solution using std::string::find

#include <iostream>

int getMatchCount(std::string a, std::string b)
{
    int match = 0;
    std::size_t pos = 0;

    while ((pos = b.find(a, pos)) != std::string::npos) {
        ++pos;
        ++match;
    }

    return match;
}

int main() {
    std::cout << getMatchCount("is", "isis");
    return 0;
}

Edit I use std::size_t since I want to compare it with std::string::npos. An int is not guaranteed to be big enough to be able to do that. std::size_t is also unsigned, which makes it a good choice to represent indexes.

If we break down the loop it runs b.find(a, pos) which will find the first occurance of the substring a in string b. The return value will be either the index of where the substring starts if a match is found or std::string::npos if it's not found.

The first time the loops run pos is 0 so we start from the beginning of string b when we search. We then store the returned value in pos. (In our example that will be 0 again for the first match)

If the returned value was not std::string::npos we found a match, so we increase match. For the next iteration of the loop we want to start the search after the last match, so we increment pos by one. (In our example, we increment it to 1, and search for the substring starting at index 1 and onward)

super
  • 12,335
  • 2
  • 19
  • 29