Q:

Write a C++ program to compare two given strings and return the number of the positions where they contain the same length 2 substring

0

Write a C++ program to compare two given strings and return the number of the positions where they contain the same length 2 substring

Sample Output:

1
1
2

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
 
using namespace std;

int test(string str1, string str2)
        {
            int ctr = 0;
            for (int i = 0; i < str1.length()-1; i++)
            {
                string firstString = str1.substr(i, 2);
                for (int j = 0; j < str2.length()-1; j++)
                {
                    string secondString = str2.substr(j, 2);
                    if (firstString==secondString) 
                    ctr++;
                }
            }
            return ctr;
        }
        
int main() 
 {
  cout << test("abcdefgh", "abijsklm") << endl; 
  cout << test("abcde", "osuefrcd") << endl; 
  cout << test("pqrstuvwx", "pqkdiewx") << endl;     
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now