Question

Write a function to check whether string s1 is a substring of string s2. The function...

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)

Write a program to test the function.

Homework Answers

Answer #1
#include <stdio.h>

int indexOf(const char *s1, const char *s2);

int main() {
    printf("%d\n", indexOf("to", "October"));
    printf("%d\n", indexOf("andy", "candy"));
    printf("%d\n", indexOf("hi", "hello"));
    return 0;
}

int indexOf(const char *s1, const char *s2) {
    int i = 0, j;
    while (s2[i]) {
        j = 0;
        while (s1[j]) {
            if (s1[j] != s2[i+j])
                break;
            j++;
        }
        if (s1[j] == 0)
            return i;
        i++;
    }
    return -1;
}

Know the answer?
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
Similar Questions
Write a function to find the last occurrence of a string within another string. The function...
Write a function to find the last occurrence of a string within another string. The function should use the prototype below. Use pointer arithmetic when traversing the strings. (C Language) char * search_string(const char * str, const char * substr);
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
The function below returns a substring of input string. What is the invariant of this function?...
The function below returns a substring of input string. What is the invariant of this function? Static string substring( String in, int ind) { String t = in ; t = t.substring(ind); return t; }
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Write a function to create a copy of a string with the prototype below. (C Language)...
Write a function to create a copy of a string with the prototype below. (C Language) char * strcpy (const char *); The function should dynamically allocate the necessary memory for the new string. You can do that with char * newstr = (char *) malloc(sizeof(char) * (strlen(str)+1)); assuming str is your input string. Think though how you would copy the characters of the string, and don't forget about the end of string character.
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by...
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by “shuffling” the letters of two strings s1 and s2. If the strings are not of equal lengths, concatenate the remaining letters of the longer string to the result. Your program should work with the code below: s1 = "abcd" s2 = "1234" s3 = "abcdefg" s4 = "123456" print(f"{s1:10s}\t{s2:10s}\t{shuffle (s1,s2)}") print(f"{s3:10s}\t{s2:10s}\t{shuffle (s3,s2)}") print(f"{s1:10s}\t{s4:10s}\t{shuffle (s1,s4)}") Output: abcd 1234 a1b2c3d4 abcdefg 1234 a1b2c3d4efg abcd 123456 a1b2c3d456
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
int truncate(char str[], unsigned int index); Create a function called truncate() that takes a string and...
int truncate(char str[], unsigned int index); Create a function called truncate() that takes a string and integer as a parameter. Your function should truncate the string by placing a NULL byte at the specified index. You should return the index passed as a parameter if successful, -1 on fail. For example, if the index given is 5, but the string is “fun”, you would return -1 because the string is not long enough.
C++: Write a function that receives a pointer to a character string consisting of only alphabets...
C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters,...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT