Question

In: Computer Science

C programming Write a function called string in() that takes two string pointers as arguments. If...

C programming

Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.

Solutions

Expert Solution

#include <stdio.h>
//function to return address if string2 is present in string 1
char* substring(char str1[],char str2[])
{
    char *start = NULL;
    int count1 = 0, count2 = 0, i, j, flag;
    while (str1[count1] != '\0')
        count1++;
    while (str2[count2] != '\0')
        count2++;
    for (i = 0; i <= count1 - count2; i++)
    {
        for (j = i; j < i + count2; j++)
        {
            flag = 1;
            if (str1[j] != str2[j - i])
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
        {
           start = &str1[i];
           return start;
        }
    }
     return start; //returns NULL if string is not present else address willl be returned
}
int main() {
     char str1[80], str2[50];
     for(int i=0;i<5;i++)
     {
        printf("Enter a string 1: \n");
        gets(str1);
        printf("Enter string 2:\n");
        gets(str2);
        char *result = substring(str1,str2); 
        if(result != NULL)
        {
            printf("%c \n",result);  //print if the first address of character where match occurs
        }
        else
         printf("Not present\n"); //if string 2 is not present
     }
}

INPUT AND OUTPUT


Related Solutions

The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT