Question

using c, Write a function called p4. The function takes 2 parameters, both of which are...

using c,

Write a function called p4. The function takes 2 parameters, both of which are C strings. p4 should return “true” (not 0) if the 2ndparameter is a substring of the 1st parameter, or “false” (0) otherwise. Forexample:

char topic[] = “computer”;
char substr[] = “put”;
printf(“%d\n”, p4(topic, substr); // prints 1
substr[0] = ‘P’;
printf(“%d\n”, p4(topic, substr); // prints 0 – case sensitive char s1[] = “ababcd”;
char s2[] = “abcd”
char s3[] = “baba”;
char s4[] = “baba”;
printf(“%d %d\n”, p4(s1,s2), p4(s1,s3)); // 1 0printf(“%d\n”, p4(s3,s4)); // 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdbool.h>

int p4(const char s[], const char t[]){
   int i = 0,j = 0;
   while(s[i]!='\0' && t[j]!='\0'){
      if(s[i] == t[j]){
         j++;
      }
      else{
         j = 0;
      }
      i++;
   }
   if(t[j] == '\0'){
      return true;
   }
   else{
      return false;
   }
}

int main(void) {
   char topic[] = "computer";
   char substr[] = "put";
   printf("%d\n", p4(topic, substr));
   substr[0] = 'P';
   printf("%d\n", p4(topic, substr));
   char s1[] = "abcd";
   char s2[] = "baba";
   char s3[] = "baba";
   printf("%d %d\n", p4(s1,s2), p4(s2,s3)); 
   return 0;
}

Process exited after 0.0308 seconds with returnvalue 0 Press any key to continue . - .

Please upvote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
using c, Write a function called p4. The function takes 2 parameters, both of which are...
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
  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

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

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

  • Write a python function alt which takes a list of strings myList as a parameter. alt...

    Write a python function alt which takes a list of strings myList as a parameter. alt then returns two strings s1 and s2 as a tuple (s1, s2). Here s1 is the concatenation of the strings in myList in even index positions, and s2 is the concatenation of the strings in myList in odd index positions. (List starts at index 0) For example, if myList = [‘My’, ‘kingdom’, ‘for’, ‘a’ , ‘horse’], then s1 = ‘Myforhorse’ and s2 = ‘kingdoma’.

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Using C++, thanks! Write a function called printAsterisks) that takes in no parameters nor returns any...

    Using C++, thanks! Write a function called printAsterisks) that takes in no parameters nor returns any value, but prints 3 rows of 4 Asterisks each. HINT: Use nested loops.:) For example: Test printAsterisks ); Result Answer: (penalty regime: 0 %) 1 |Void prinAsterisks() 4

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

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