Question

Function name : contains_substring Parameters : string (string), substring (string) Returns: bool Description : If the...

Function name : contains_substring

Parameters : string (string), substring (string)

Returns: bool

Description : If the substring is entirely contained within the string, then return True, otherwise return False. An empty substring will always return False. The substring may be longer than the string. You are not allowed to just write “if substring in string”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

#include<stdio.h>
#include<math.h>
#include<limits.h>
#include <stdbool.h>
#include<stdlib.h>
#include<stddef.h>

#define size sizeof(int)*100

//Prototype declaration..
bool contains_substring(char *string, char *substring);
int findLength(char *str);

int main( void )
{  
   char *string = NULL;       //String
   char *substring = NULL;       //Substring..
   //Dynamic Memory allocation..
   string = (char *) malloc (size * sizeof (char));
   substring = (char *) malloc (size * sizeof (char));
  
   printf(" Enter the string : ");
   fgets(string, size, stdin);   //Read string 1...
   printf(" Enter the sub-string : ");
   fgets(substring, size, stdin);   //Read substring..
  
   //Func call to find the substring..
   if(contains_substring(string, substring)) {
       printf(" Substring found..!! ");
   }
   else{
       printf(" Substring not found..!! ");
   }
   free(string);                  //Freeing the memory
   free(substring);
return 0;
}
          
/**
* Function to find the substring in a strings..
* Returns the true/fasle of type boolean..
*/
bool contains_substring(char *string, char *substring)
{
   int i = 0;
   int j = 0;
   int count =0;       //Counts the length of continuos mnatching chars

   int stringLength = findLength(string);   //Find & Stores Lenght of String1
   int substringLength = findLength(substring); //Find & Stores Lenght of String2
  
   if(stringLength < substringLength)          //If string2 > string1  
       return false;

   //Travesing the string1 char by char and finding the substring..
   for (i = 0; *(string + i) != '' ; i++) {  

       if(*(string + i) == *(substring + j)) {
           count++;
           j++;
       }
       else {
           i -= count;       //Make 'i' to point the previous position
           count = 0;       //If not equal start from first..
           j = 0;
       }
       if (substringLength == count ) { //Sub string present if equals..
           return true;   //Checking the length for match..      
       }
   }
   return false;       //Return false if substring not found..
}

/**
* Function to find the length of the strings..
* Returns the length of type int..
*/
int findLength(char *string)
{
   int length= 0;
  
   while(*(string+ length) != '') {
       length++;
   }
   length--;   //Omits last null char..
return length;
}

Code Screenshots:

Sample Output:

Following shows how the program works..

                

Output:

                     

______________________________________________

Please comment if required any additional changes or modifications on this..

Kindly up-vote if satisfied..

Add a comment
Know the answer?
Add Answer to:
Function name : contains_substring Parameters : string (string), substring (string) Returns: bool Description : If the...
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
  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • Problem 1 Substring matching is the process of determining whether shorter string (the substring) is contained...

    Problem 1 Substring matching is the process of determining whether shorter string (the substring) is contained within a longer string. Substring matching plays important roles in the reconstruction of an unknown DNA string from pieces, and in searching for interesting substrings within a known DNA string. Python provides a find (substring, start, end) string method that returns the lowest index (integer) where the substring is found in the index range start <= index < end. The start and end arguments...

  • bool process_words(map_type&, string); Takes in an empty map and a string representing a file name. The...

    bool process_words(map_type&, string); Takes in an empty map and a string representing a file name. The function opens the file, reads the file one line at a time, splits the line, cleans each word and then records it in the map where the key of the map is the string and the value of the map is how many times that string occurred. Error: if the file represented by the string cannot be opened, the function returns false and makes...

  • Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description:...

    Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description: You're texting your friend when you notice that they replace many letters with numbers. Out of curiosity, you want to find the product of the numbers. Write a function that takes in a string sentence and an int num, and find the product of only the first num numbers in the sentence. If num is 0, return 0. If num > O but there...

  • Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that...

    Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that takes an input string, and returns a the unscrambled version of the argument. To unscramble the string: When the string has an odd number of characters, middle character is the first character in the unscrambled result Pairs of remaining characters are added to the result, proceeding left to right from inner-most to outer-most characters. Example Call: unscramble('3cis1') Expected result: ics31 Example Call: unscramble('ocicssol') Expected...

  • Function name​: quick_maths() Parameters: a number (float), a number (float), an operation (string) Returns: a float...

    Function name​: quick_maths() Parameters: a number (float), a number (float), an operation (string) Returns: a float Description: You’re trying to do your math homework, and you realized that you can write a program to do all the simple operations! The five possible operations are “+”, “-”, “*”, “/”, and “%”. Based on the operation that is passed in, perform the operation on the two numerical parameters. The first parameter should always go first in the operation. Return the result of...

  • sorted vowels Write a function that returns true if the vowels in the input string A...

    sorted vowels Write a function that returns true if the vowels in the input string A appear in non-decreasing order and false otherwise. You can assume this function is not case sensitive. If the string does not contain any vowels, it must return true. bool sorted_vowels (const char A)

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

  • C++ Programming: Write a function that takes in a C++-style string s (from #include ), as...

    C++ Programming: Write a function that takes in a C++-style string s (from #include ), as well as an integer n, and then returns true if any substring of length n in s repeats itself, and false otherwise. For example, if s is "toe-to-toe" and n is 3, then the function would return true since "toe" occurs twice within s. However, if n were 4, the function would return false. As a second example, if s is "singing" and n...

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following 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. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

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