Must use loops. Please do not use sequence of if statements.
I need it in C++.
Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
for input of "xxcaazz", "xxbaaz" → 3
for input of "abc", "abc" → 2
for input of "abc", "axc" → 0
#include <iostream>
#include <string.h>
using namespace std;
//function to count the matching string of length 2
int numOfSubString(const char s1[], const char s2[])
{
//find the string length
int lenS1 = strlen(s1);
int lenS2 = strlen(s2);
int len;
//find lowest length
if(lenS1<lenS2)
len = lenS1;
else
len = lenS2;
int count = 0;
//count the substring of length 2
for(int i=0; i<len-1; i++)
{
if(s1[i] == s2[i] && s1[i+1] == s2[i+1])
{
count++;
}
}
//return statement
return count;
}
int main()
{
//function calling
cout<<numOfSubString("xxcaazz", "xxbaaz")<<endl;
cout<<numOfSubString("abc", "abc")<<endl;
cout<<numOfSubString("abc", "axc");
return 0;
}
OUTPUT:
3
2
0
Get Answers For Free
Most questions answered within 1 hours.