Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ F7 Search an array for a value. 15pts. Task -> Implement the function specifications, i.e. the prototypes. Search a sentence for the occurrence of

C++

F7 Search an array for a value. 15pts.

Task -> Implement the function specifications, i.e. the prototypes.

Search a sentence for the occurrence of a pattern. Report the position where the pattern occurs. It might not occur, occur only once, or could occur many times. Find all the matches and output their position in the sentence. The position starts at index=0 at the beginning of the sentence.

See the sample test case for input and output formats.

/* * Purpose: Searching for multiple occurrence of patterns * Note: cout proceeds to null terminator, cin reads to end of line * for character arrays * */

//System Libraries Here #include //cin,cout,getline() #include //strlen() using namespace std;

//Function Prototypes Begins Here //srch1 utility function Input->start position, Output->position found or not //srch1 is a simple linear search function, repeat in srchAll till all found //srch1 Input->sentence, pattern, start position Output-> position found //Remember arrays start at index/position 0 //srchAll Input->sentence, pattern Output->position array int srch1(const char [],const char [],int);//Search for 1 occurrence void srchAll(const char [],const char [],int []);//Search for all occurrences void print(const char []);//Print the character arrays void print(const int []); //Print the array of indexes where the pattern found

//Program Execution Begins Here int main(int argc, char** argv) {

//Declare all Variables Here const int LINE=81; //Size of sentence or pattern to find char sntnce[LINE],pattern[LINE]; //80 + null terminator int match[LINE]; //Index array where pattern was found

//Input a sentence and a pattern to match cout<<"Match a pattern in a sentence."<

//Search for the pattern //Input the sentence and pattern, Output the matching positions //Remember, indexing starts at 0 for arrays. // initialize match array with -1 memset(match, -1, sizeof(match)); srchAll(sntnce,pattern,match);

//Display the inputs and the Outputs cout<

//Exit return 0; }

int srch1(const char sntnce[],const char pattern[], int start_pos) { int slen = strlen(sntnce); // length of sentence int plen = strlen(pattern); // length of pattern

int i, j; for (i = start_pos; i < (slen-plen+1); ++i) { // check if pattern exists starting from ith index in sentence for (j = 0; j < plen; j++) { if (sntnce[i+j] != pattern[j]) break; }

// return first index from which pattern was found after starting search from start_pos if (j >= plen) return i; }

// return -1 if pattern not found return -1; }

void srchAll(const char sntnce[],const char pattern[], int match[]) { // searches for all patterns in sentence one by one int k = 0; int pattern_index = 0; while ((pattern_index = srch1(sntnce, pattern, pattern_index)) != -1) { match[k++] = pattern_index; pattern_index += 1; } }

void print(const char str[]) { // prints the char array for (int i = 0; str[i] != 'i'; ++i) cout << str[i]; cout << endl; }

void print(const int match[]) { // prints indexes where pattern was matched for (int i = 0; match[i] != -1; ++i) cout << "Pattern found at index " << match[i] << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago