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...
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...
(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...
(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.
A DNA string is a sequence of the bases a, c, g, and t in any...
A DNA string is a sequence of the bases a, c, g, and t in any order, whose length is usually a multiple of three. In reality, it is not necessarily a multiple of three, but we will simplify it as such for discussion. For example, aacgtttgtaaccagaactgt is a DNA string with a length of 21 bases. Recall that a sequence of three consecutive letters is called a codon. Assuming the first codon starts at position 1, the codons are...
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT