Question

In: Computer Science

In C: Find a string within a string Given two strings S1 & S2, search for...

In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]. Character comparisons will be case-sensitive.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <stdio.h>

#define MAX_SIZE 10000

int findSubstrIndex(char S1[], char S2[]);

int main()

{

    char S1[MAX_SIZE], S2[MAX_SIZE];

    int index;

    printf("Enter String1: ");

    scanf("%[^\n]s", S1);

    printf("Enter String2: ");

    scanf("\n%[^\n]s", S2);

    index = findSubstrIndex(S1, S2);

    if (index == -1)

        printf("%s not found!\n", S2);

    else

        printf("%s found at index %d\n", S2, index);

    return 0;

}

int findSubstrIndex(char S1[], char S2[])

{

    int i = 0, j;

    while (S1[i] != '\0')

    {

        if (S1[i] == S2[0])

        {

            int exists = 1;

            j = 1;

            while (S2[j] != '\0')

            {

                if (S2[j] != S1[i + j])

                {

                    exists = 0;

                    break;

                }

                j++;

            }

            if (exists){

                return i;

            }

        }

        i++;

    }

    return -1;

}


Related Solutions

Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
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...
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.
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +. (b) Analyze time and space of part (a) (c)Implement part (a) in your favorite programming language without using 0+0 operator. You should read the two numeric strings from a FILE input.in and output their addition to standard output. You Solution will be test on 10,000 test cases. Each test case will include...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT