Question

In: Computer Science

(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).

Solutions

Expert Solution

Here is your c++ code for above problem. I have successfully executed this code using TurboC++ compiler.Header file inclusion statements may be different with different compiler.You can easily adjust it according to your compiler.

#include<iostream.h>
#include<stdio.h>
int indexOf(const char *s1, const char *s2) //Function definition
{
int m,n;
for(m=0;*(s1+m)!='\0';m++); //finding length of string s1
for(n=0;*(s2+n)!='\0';n++); //finding length of string s2

for (int i = 0; i <= n - m; i++) {
   int j;

   for (j = 0; j < m; j++) //loop to check entire substring in given string
   if (s2[i + j] != s1[j])
       break;

   if (j == m)
   return i;
}

return -1;
}

int main()
{

char s1[10];
char s2[100];
cout<<"Enter the substring to be search:";
gets(s1); //getting substring from user
cout<<"Enter the string:";
gets(s2); //getting string from user
int res = indexOf(s1, s2);
if (res == -1)
   cout << "Not present";
else
   cout << "Present at index " << res;

return 0;
}

Here is your output:


Related Solutions

Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
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....
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT