Given two strings, not necessarily of the same length, count the number of positions at which they differ.

Given two strings, not necessarily of the same length, count the number of positions at which they differ.
No, you.
We generally refuse to completely solve homework questions, like what you're asking us to do. I wrote out a few common reasons in this article, if you'd like to read it:
http://cplusplus.com/articles/DjGEy60M/

Good luck with your problem, and no hard feelings. :)

-Albatross
Last edited on
no problem thanks but it was not a part of a homework
For same-size strings, this is trivially implemented as inner_product:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <numeric>
#include <functional>
using namespace std;
int main()
{
    string s1 = "aaaaaaaaaaaaaaaaaaa";
    string s2 = "aaaAaaaAAaaaaBaaaaa";
    std::cout << inner_product(s1.begin(), s1.end(), s2.begin(), 0,
                               plus<int>(), not_equal_to<char>()) << '\n';
}        


But for different length strings, there's not enough information: are you counting the mismatches in the common prefix? (in which case you'd just use min(s1.size(), s2.size()) above). Are you looking for the substring in the larger string that would have the smallest number of mismatches with the smaller string? (more complicated algorithm). Are you looking for something else entirely?
Last edited on
@cubbi we have to count at how many positions the strings differ from each other i think u have the right code thanks
Topic archived. No new replies allowed.