Question: Can someone please look over this before I submit it and correct whatever mistakes? Write a C program find.c that includes a main program and
Can someone please look over this before I submit it and correct whatever mistakes?
Write a C program find.c that includes a main program and a function with the following function prototype: int find(char *text, char *pattern); The function accepts two text strings, text and pattern as input. It then determines the location of the specified pattern string in the text string. If the pattern occurs multiple times, it returns the first occurrence of the pattern and if it does not occur, then the function returns -1. For example, if called with the text Hello, World! and the pattern orl, the function returns 8 because the pattern starts at position 8 in the text string. If you search pattern "abc" it will return -1. The main program must test the function by calling it with 3 different values for text and pattern and printing the results to the screen using the following format string: "Pattern %s occurs in %s at the location %d. " Remember to catch -1 () as the return value and print: "Pattern %s has not been found in %s. "
My answer is:
In C programming language
#include
printf("Enter the first string : "); fgets(str, sizeof(str), stdin); str[strlen(str)-1]='\0'; printf("Enter the second string : "); fgets(str2, sizeof(str2), stdin); str2[strlen(str2)-1]='\0'; int found = subString(str,str2); if(found != -1) printf("Pattern %s occurs in %s at the location %d. ",str,str2,found); else printf("Pattern not found. "); return 0; } int subString(char *s1,char *s2) { int count1=0,count2=0,i,j,mark;
count1 = strlen(s1); count2 = strlen(s2);
for(i=0;i<=count1-count2;i++) { for(j=i;j
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
