Question

Download the file patternMatch.c from the course website. This file contains part of a program to do pattern matching. The fu
Use the following brute-force algorithm to find a match. First, compare the pattern against the text starting with the first
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code is given below:

#include<stdio.h>
// read line function
int readline(char str[],int maxchar) {
   char ch;
   int ind = 0; // for calculating the length
   scanf("%c",&ch);
   while(ind<40 && ch!='\n') { // this will read the characters until a newline is entered
       str[ind] = ch;
       scanf("%c",&ch);
       ind++;
   }
   return ind; // the ind will be the length of the string
}
// for finding the match
char* findmatch(char pattern[], char text[],int patternlength, int textlength) {
   for(int i=0;i<=(textlength-patternlength);i++) { // we go to the last possible index of match which is (textlength-patternlength)
       if((pattern[0] == '?')||(text[i] == pattern[0])) { // if the fist char of pattern match or it is a wildcard then there can be a match
           int j = 0, k = i;
           for(;(j<patternlength) && (k<textlength);j++,k++) {// now we match character by character
               if(pattern[j] == '?') // if there is a wildcard in pattern, it's okay
                   continue;
               else if(pattern[j] != text[k]) { // otherwise if it does not match then no match
                   break;
               }
           }
           if(j == patternlength) // if we traversed whole pattern it means it did not break, which means there is a match at index i.
               return &text[i]; // so return address of match
       }
   }
   return NULL; // otherwise return null
}
void printmessage(char *position, char text[],int patternlength, int textlength) {
   if(position == NULL)
       printf("no match\n");
   else {
       int ind = (position - text); // index will be the difference of base address and address of match
       printf("The pattern was found at char %d. ",ind + 1);
       printf("The remaining text chars are: ");
       ind += (patternlength); // go to the end of match
       while(ind<textlength) {
           printf("%c",text[ind]); // printing the chars after match
           ind++;
       }
       printf("\n");
   }
}
int main() {
   char text[40], pattern[40], *position;
   int textlength, patternlength;

   printf("Enter text: ");
   textlength = readline(text, 40);
   printf("Enter Pattern: ");
   patternlength = readline(pattern, 40);
   position = findmatch(pattern, text, patternlength, textlength);
   printmessage(position, text, patternlength, textlength);
}

Sample input and output:
chandan@chandan-Vostro-15-3568:~$ ./a.out Enter text: abcdefghi Enter Pattern: ?def? The pattern was found at char 3. The rem

Screenshot of the code is given below:
#include<stdio.h> int readline(char str[], int maxchar) { char ch; int ind = 0; scanf(%c,&ch); while(ind<40 && ch!=\n) {

, ind + 1); I PUSILIUIL -= NULL) printf(no match\n); else { int ind = (position - text); printf(The pattern was found at

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Download the file patternMatch.c from the course website. This file contains part of a program to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Write the following program in C Language using standard library functions. /*Implement the Find function, called...

    Write the following program in C Language using standard library functions. /*Implement the Find function, called in the program below. This function receives two strings, and looks for the first occurrence of the second string in the first string, returning the number of characters it is in, relative to the beginning of the first string. If not, returns -1. In the program, a string with two news headlines is given, and the sub-strings "iPad" and "Huawei” are searched for, having...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT