Question

****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.

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

Answer:

#include<stdio.h>
#include<string.h>
int main()
{
   const char substring[]="cd";//declaring string constant what we need to search
   const char string[]="abcdefgh";//declaring a text where we need to search
   int len1,len2,i,j,flag,ind=-1;
   len1=strlen(substring);//finding the length of the searching string
   len2=strlen(string);//finding the length of the textstring
   flag=1;
   for(i=0;i<=(len2-len1);i++)//These are the possiblities that the string may present
   {
       flag=1;
       for(j=i;j<(i+len1);j++)//we need to check length of the substring everytime
       {
           if(string[j]!=substring[j-i])//checking each character of substring is present in the string everytime
           {
               flag=0;
               break;  
           }
           ind=i;  
       }
       if(flag==1)
           break;  
   }
   if(flag==1)//if the string is present in text
       {
           printf("Search successful at index %d",ind);
       }
   else
       printf("Search unsuccessful");//string is not present in the text
   return 0;
}

output:

IF you have any doubts..please comment..Thank you..

Add a comment
Know the answer?
Add Answer to:
****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...
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
  • 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...

  • C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C...

    C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C Strings: char *strStr(const char *s1, const char *s2) - Locates the string s2 as a substring of s1 and returns a pointer to its first occurrence in s1 or nullptr if s2 is not a substring of s1 char *strrChr(constant char *s, int ch) - Returns a pointer to the last occurrence of the character ch in string s or nullptr if ch is...

  • C programming 1 String Merging Write a program that reads two strings s1 and s2 from...

    C programming 1 String Merging Write a program that reads two strings s1 and s2 from the keyboard and merges them to a string s3. Strings s1 and s2 are statically allocated whereas s3 is dynamically allocated to fit exactly s1 and s2. The two original strings are no longer than 100 characters long. Use the following function to merge the two strings. char *stringMerge(char s1[], char s2 []); Example: Enter the first string: Hello world Enter the first string:...

  • Write a Java program for all items in Question Use System.out.println() for each resulting string. Let...

    Write a Java program for all items in Question Use System.out.println() for each resulting string. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to...

  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1...

    Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1 and S2, determine if S2 is a sub string of S1. Sub string example: S1: ‘I am happy today’, S2: ‘happy’, S2 is a substring of S1. S1: ‘I am happy today’, S2: ‘sad’, S2 is not a substring of S1. Call above procedure in an assembly language program and show the result of the procedure.

  • Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that...

    Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I am sure we’ve all done this many times when working with Word, Notepad, or other editors. Since we don’t have a GUI or other means of displaying the contents of a file all at once, let’s modify the problem slightly. Rather than locating a specific substring within a...

  • Write a function definition for a function my_strcmp() with the following header: Write/Implement a function definition...

    Write a function definition for a function my_strcmp() with the following header: Write/Implement a function definition for a function my_strcmp () with the following header: int my_strcmp (const char *s1, const char *s2) This function compares the string pointed to by s1 to the string pointed to by s2. If the string pointed to by s1 comes before the string pointed to by s2 in dictionary ordering, then -1 is returned. If the string pointed to by s1 is the...

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

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