Question

In: Computer Science

Implement a substring search function which searches a string for a given keyword. The function should...

Implement a substring search function which searches a string for a given keyword. The function should return a pointer to the address of the first character of the substring within the original string, if it exists. If the substring is not found within the string, return NULL. If the input is found, print "Key found." otherwise print "Key not found." Your program’s input and output should match what is seen in the example run. • You may only use stdio.h. • Save your code as prob3.c.

Example Run

Enter string: this is a test string.

Enter key: is

Key found.

Solutions

Expert Solution

#include <stdio.h>

char *substring(char *string, char *key);

int main()
{
   char str[100], key[100];
   printf("Enter String: ");
   gets(str);
   printf("Enter Key: ");
   gets(key);
   char *addr = substring(str,key);
   return 0;
}

char *substring(char *string, char *key)
{
   int l,i,j;
   for (l = 0; key[l] != '\0'; l++);

for (i = 0, j = 0; string[i] != '\0' && key[j] != '\0'; i++)
   {
       if (string[i] == key[j])
       {
           j++;
       }
       else
       {
           j = 0;
       }
}
  
   if (j == l)
   {
       printf("Key found.");
   }
   else
   {
       printf("Key not found.");
   }
   return &string[i-j];
}


Related Solutions

I'm working on python code which is substring matching. For example, the given string is input...
I'm working on python code which is substring matching. For example, the given string is input "CTTGTGATCTCGTGTCGTGGGTAG", and a substring we want to find in the main one is "GTGG". So the code will print out the position and the substrings in the main which has exactly one different position. For example, start at position 4 the substring is GTGA since there is only one letter different, and so on. Even though we know that string index starts at 0,...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take...
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take the following parameters: • F: A function (assumed to be continuous) whose roots you want to find, • a: A floating-point number giving the left endpoint of the initial interval in which you want to search for a root of F. • b: A floating-point number giving the right endpoint of the initial interval. • delta: A non-negative floating-point number giving the acceptable proximity...
Implement the following function by replacing the pass keyword with your code. Do not change the...
Implement the following function by replacing the pass keyword with your code. Do not change the spelling of the function name or the parameter list. def order_pie(): Important notes: Ask ONLY these questions, in EXACTLY this order. Do not ask more questions than are necessary to arrive at the right answer. You can expect the input values to be 'yes' or 'no'. For this assignment you do not have to worry about other spellings or capitalization. The return values must...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write test programs to convince someone that you did them right. DIRECTIONS: You have been provided: 1. Function stubs for searching and sorting algorithms: Utility functions: swap, shift right/left, show array, insertion point. Searching: linear seach, binary search Sorting: bubble sort, insertion sort, selection sort. 2. Main program providing a pattern for testing youour functions, including: - sample arrays, each providing a different test case....
Visual Studio Basic 1: Which string function you will be likely using to search for a...
Visual Studio Basic 1: Which string function you will be likely using to search for a certain pattern in a string? Describe your answer. 2: If you are to find the occurrence of 4-letter string “abcd” regardless of its case combinations, such as “AbCd”, “ABCd” and etc, in a long string, what would you do? Describe your answer.
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Description: The search method should prompt the user for a search phrase. Given the search phrase...
Description: The search method should prompt the user for a search phrase. Given the search phrase the search method should search each line in the file for the location of the phrase on each line. If the search phrase is not found on a line then just go to the next line. The search method must find every occurrence of the phrase on a line. The search method should print the line number, the line and the location of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT