Question

In: Computer Science

THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...

THE STRING MATCH PROBLEM

C++ only. Must use loops. Please do not use sequence of if statements. .

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

Solutions

Expert Solution

Here, we want to find the number of positions where they contain the same length 2 substrings. So, for this we follow the following algorithm:

  1. Loop through the first string and hold the substring.
    string aSubString = a.substr(i, 2);
  2. Now loop through the second string, hold the substring for the second string and check whether the substring which we are holding from the first string matched from the substring of the second string or not.
    string bSubString = b.substr(j, 2);
    if (aSubString == bSubString) { //counter. }
  3. If substrings are matching, then increase the counter by 1.
    c++; // Here c is the counter.
  4. Return the counter at the end of the program.


Please refer to the comments of the program for more clarity.

#include<bits/stdc++.h>
using namespace std;

int countMatching(string a, string b) {  // Function to find the number of matching sub-strings
    int c = 0; // Initialized the counter by 0

    for (int i = 0; i < a.length() - 1; i++) {  // Loop for string a

        for (int j = 0; j < b.length() - 1; j++) {  // Loop for string b
            string aSubString = a.substr(i, 2);    // Finding substring of a for matching
            string bSubString = b.substr(j, 2);    // Finding the substring of b for matching
            if (aSubString == bSubString) {
                c++;                               // If matching then increase the counter by 1
            }
        }
    }
    return c;  // Returning the answer
}

int main()
{
    /*
        Here we are taking string a and string b as input, you can also replace it with the initialized strings.
    */
    string a,b;
    cin >> a >> b;
    int ans = countMatching(a, b); // Calling the function
    cout << ans << endl;           // Printing the result
    return 0;
}
/*
Sample Input/Output:

Input:
xxcaazz
xxbaaz

Output:
3
*/

I am also attaching sample inputs and outputs for your reference.

Please let me know in the comments if you have any confusion. Also, please upvote if you like.


Related Solutions

THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I need it in C++. Given an input string, set result to a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceded or followed by a letter -- so for example the "is" in "this" does not count.   • for input of "is test" → "is not test" • for input...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and similar role-playing games, dice with different numbers of sides are used to determine the outcomes of events throughout the game. There are different dice for different occasions: 4-sided, 6-, 8-, 10-, 12-, and 20-sided dice are common. A required roll is typically designated <n>d<s> where <n> is the number of dice to throw and <s> is the number of sides on those dice. For...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
Code in c++, do not use loops, make it as simple as it can be. Thanks!...
Code in c++, do not use loops, make it as simple as it can be. Thanks! Background Well it has finally happened; AMC’s “The Walking Dead” has become a reality. The zombie apocalypse took place. It has been a couple of years since we have started to “rebuild” our society, and now is the time for you to be able to shine. We have come across technology that will allow for us to get back to life as it once...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program simple. Q (5) Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely inserted_text (of type string), characters_count (of type unsigned int), words_count...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT