Starting from:

$30

CS201-Homework 3 String Matching Solved

In this homework, you will write a console application that would take two string values as input and search one of them in the other.

The C++ program that you are going to implement will operate based on the selection number input by the user. According to the selection your program will either continue to get more inputs or exit the execution. The input can either be a source string or a search string and your program will search the latter string within the source string. If your program finds the search string in the source string it will output the index where the search string is found and display that index along with the search string to the console (all of the results will be printed out if multiple matches exist). There are some rules for the input strings and selection choices by the user. Details will be explained later in the document.

 

Details and Rules
First of all, your program will display the possible selections with example source and search strings in the beginning of the program, where there will be three choices. If selection 1 or 2 is input by the user, then you should take the source or search string as input respectively and wait for the next input. If the user enters 3 as the selection, then you should exit and end the program. If the user inputs a wrong selection number, then you should print an error message and continue the program waiting for the next input.

Secondly, when the user inputting the strings or selections you should do various input checks against all possible errors. These will be explained in the following section. Your program should only end if the user enters the selection 3 to exit.

Thirdly, your program will accept strings, that consist of ONLY lowercase letters as source string. This rule holds for the search string too except for one character, which is ‘?’. Your search string may contain the ‘?’ (Question Mark) character as a wild card, i.e. this character matches any character.

Finally, when your program matches the search string in the source, it should print the starting index point of the match and also the matched string.

Input Entry and Input Check
●     Selection choice MUST be one of {1, 2, 3}. Any other digits or strings are invalid input. HINT: You may take choice from user as a string for the sake of simplicity.

●     Source string o            MUST NOT be empty o           MUST NOT contain ‘?’

MUST NOT contain any character except lower case letters ‘a’-‘z’

●     Search stringo

o        MUST NOT be empty,

o        MUST NOT be longer than the source string, oo MUST NOT be ONLY ‘?’s

MUST NOT contain any character except lower case letters ‘a’-‘z’ and ‘?’

If any of the inputs are not valid, your program should display an error message and expect a new input from the user.

Use of Functions and Other Rules
Unlike the second homework, we will not specify any functions here. But you are expected to use functions to avoid code duplication and improve the modularity of your program. If your main function or any user-defined function is too long and if you do everything in main or in another user-defined function, your grade may be lowered.

AND PLEASE DO NOT WRITE EVERYTHING IN MAIN AND THEN TRY TO

SPLIT THE TASK INTO SOME FUNCTIONS JUST TO HAVE SOME FUNCTIONS

OTHER THAN MAIN. THIS IS TOTALLY AGAINST THE IDEA OF FUNCTIONAL DESIGN AND NOTHING BUT A DIRTY TRICK TO GET SOME POINTS. INSTEAD

PLEASE      DESIGN     YOUR     PROGRAM     BY     CONSIDERING    NECESSARY

FUNCTIONS AT THE BEGINNING.

Try to use parametric functions and avoid inputs in functions. Do NOT use any global variables (variables defined outside the functions) to avoid parameter use.

As in the previous homework, no abrupt program termination is allowed in the middle of the program! The program flow should continue until the end of the main function.

Sample Runs
Below, we provide some sample runs of the program that you will develop.

NOTE THAT, we did not upload all of the queries in the sample runs to GradeChecker. So, you may want to check all of queries manually.

Sample Run 1
This program searches a search string in a source string with the option of wildcards.

Example inputs with their meaning in parantheses:

1  thislectureisawesome (to enter a new source string)

2  lecture (to enter a new source string) 2 t?r? (to enter a source string with wildcards)

3  (to exit the program)

Enter your choice and string: 1 changingthesourcestring

Source word has been changed to "changingthesourcestring"

Enter your choice and string: 2 the

For the source word "changingthesourcestring" and search word "the", "the" has been found at index 8

Enter your choice and string: 2 ing

For the source word "changingthesourcestring" and search word "ing",

"ing" has been found at index 5

"ing" has been found at index 20

Enter your choice and string: 2 i?g

For the source word "changingthesourcestring" and search word "i?g",

"ing" has been found at index 5

"ing" has been found at index 20

Enter your choice and string: 2 is

For the source word "changingthesourcestring" and search word "is", no match has been found.

Enter your choice and string: 2 ?es

For the source word "changingthesourcestring" and search word "?es",

"hes" has been found at index 9

"ces" has been found at index 15

Enter your choice and string: 2 ?e?

For the source word "changingthesourcestring" and search word "?e?",

"hes" has been found at index 9

"ces" has been found at index 15

Enter your choice and string: 3

See you!

Sample Run 2
This program searches a search string in a source string with the option of wildcards.

Example inputs with their meaning in parantheses:

1  thislectureisawesome (to enter a new source string)

2  lecture (to enter a new source string) 2 t?r? (to enter a source string with wildcards) 3 (to exit the program)

Enter your choice and string: 1 newsourcestring

Source word has been changed to "newsourcestring"

Enter your choice and string: 1 newsourcestringv2 Wrong input format! Try again.

Enter your choice and string: 1 newsourcestringvtwo Source word has been changed to "newsourcestringvtwo"

Enter your choice and string: 2 ? Wrong input format! Try again.

Enter your choice and string: 2 ??? Wrong input format! Try again.

Enter your choice and string: 2 erf2 Wrong input format! Try again.

Enter your choice and string: 2 sou*ce Wrong input format! Try again.

Enter your choice and string: 2 ?o

For the source word "newsourcestringvtwo" and search word "?o",

"so" has been found at index 3

"wo" has been found at index 17

Enter your choice and string: 3 See you!

Sample Run 3
This program searches a search string in a source string with the option of wildcards.

Example inputs with their meaning in parantheses:

1  thislectureisawesome (to enter a new source string)

2  lecture (to enter a new source string) 2 t?r? (to enter a source string with wildcards)

3  (to exit the program)

Enter your choice and string: 2 sabanci Wrong input format! Try again.

Enter your choice and string: 1 helloworld? Wrong input format! Try again.

Enter your choice and string: 1 thisissourcestring

Source word has been changed to "thisissourcestring"

Enter your choice and string: 2 this

For the source word "thisissourcestring" and search word

"this",

"this" has been found at index 0

Enter your choice and string: 2 ?i?

For the source word "thisissourcestring" and search word

"?i?",

"his" has been found at index 1

"sis" has been found at index 3

"rin" has been found at index 14

Enter your choice and string: 2 t?

For the source word "thisissourcestring" and search word "t?",

"th" has been found at index 0

"tr" has been found at index 13

Enter your choice and string: 2 thisissourcestring

For the source word "thisissourcestring" and search word

"thisissourcestring",

"thisissourcestring" has been found at index 0

Enter your choice and string: 2 thisissourcestrin?

For the source word "thisissourcestring" and search word

"thisissourcestrin?",

"thisissourcestring" has been found at index 0

Enter your choice and string: 2 ?hisissourcestrin?

For the source word "thisissourcestring" and search word

"?hisissourcestrin?",

"thisissourcestring" has been found at index 0

Enter your choice and string: 5 Choice can be 1, 2 or 3! Try again.

Enter your choice and string: Cs201 Choice can be 1, 2 or 3! Try again.

Enter your choice and string: 3

See you!

Sample Run 4
This program searches a search string in a source string with the

option of wildcards.

Example inputs with their meaning in parantheses:

1  thislectureisawesome (to enter a new source string)

2  lecture (to enter a new source string)

2  t?r? (to enter a source string with wildcards)

3  (to exit the program)

Enter your choice and string: 1 sabanciuniversity

Source word has been changed to "sabanciuniversity"

Enter your choice and string: 2 university

For   the            source word "sabanciuniversity" and search word

"university",

"university" has been found at index 7

Enter your choice and string: 2 ab?n

For the source word "sabanciuniversity" and search word "ab?n", "aban" has been found at index 1

Enter your choice and string: 2 uni?er

For the source word "sabanciuniversity" and search word "uni?er", "univer" has been found at index 7

Enter your choice and string: 2 un??er

For the source word "sabanciuniversity" and search word "un??er",

"univer" has been found at index 7

Enter your choice and string: 2 u?i?er

For the source word "sabanciuniversity" and search word "u?i?er", "univer" has been found at index 7

Enter your choice and string: 2 s???

For the source word "sabanciuniversity" and search word "s???",

"saba" has been found at index 0

"sity" has been found at index 13

Enter your choice and string: 1 university

Source word has been changed to "university"

Enter your choice and string: 2 sabanciuniversity Wrong input format! Try again.

Enter your choice and string: 2 university

For the source word "university" and search word "university",

"university" has been found at index 0

Enter your choice and string: 3

See you!

More products