Question

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 string_contains() which takes pointers two strings as inputs and determines whether the first string is a subset of the second string. It returns the index in the second string where a match is found. If the first string is not a subset of the 2nd string, it should return -1. You are not allowed to use built-in functions (other than strlen()) for this task. Example: If the two strings are defined like shown below, char str1 = "world"; char str2 = "hello world"; the function call string_contains(str1, str2) should return 6, i.e. the index of the location where the match is found. The function has the following prototype: int string_contains(char * str1, char * str2);

Task 4: Partition an array for use with ‘ksmall’ algorithm. For this task you have to write the C function, partition_array(), that partitions the data around a pivot value. You will be implementing Step 1 of a famous algorithm ‘ksmall’ for this task. The algorithm has two basic steps. Step 1: Partition the data around a pivot value such that all the elements to the left of pivot index are smaller than the pivot value and the ones to the right are greater than the pivot value. Step 2: Recursively call the function ‘kSmall()’, on the partition that contains the kth smallest element. Details are given in the attached document. (you don’t have to worry about this step) For this task you will develop the function partition_array(). The partition algorithm chooses some element p (called the pivot), then rearranges the array such that: • All elements less than or equal to p are before p. • All elements greater than p are after p. • p is in the position it would occupy if the array were sorted. • The algorithm then returns the index of p. • For this implementation your algorithm will always choose the last element of the input array as pivot. It has the following prototype: int partition_array(float * ptr_array, int size);

Please code in C and use recursion

Homework Answers

Answer #1

Here is the solution of your task 1

Here is the code

#include <stdio.h>
#include<string.h>
//function defination of compare_string
int compare_string(char str1[],char str2[])
{ //declaration of relevant variables
int l1,l2,i=0;
while(1)
{ //if both string are reached to last character and still equal return 0
if(str1[i]=='\0' &&str2[i]=='\0')
return 0;
//if character are not equal return the difference
if(str1[i]!=str2[i])
{   
return (str1[i]-str2[i]);
}
i=i+1;
}
  
}
int main()
{ int diff; char str1[]="hello",str2[]="hey";
diff=compare_string(str1,str2);
printf("differnce between string is: %d",diff);

return 0;
}

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
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const...
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const char* str2) which returns the sum of strlen(str1) and strlen(str2). You can assume that strlen(str2) is always strictly greater than strlen(str1) (strlen(str2) > strlen(str1)). You MAY NOT use any function from string.h. You MAY NOT use any square brackets([]) or any type of loops in function.
C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as...
C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as below. char *str1 = “my name is Amy!”; char *str2 = “I got lost on my way to work”; The function accepts two constant character strings, str1 and str2 as pointer. Exchange the sentence of the variables str1 and str2 and then compute and display both strings’ length using pointer. Return the sum of the lengths of str1 and str2 int getStringInfo(const char *str1,...
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Question Recursive Maximum: In this question you must develop and use a recurive function to find...
Question Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. Note that the function max is not recursive....
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...