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
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;
}
Get Answers For Free
Most questions answered within 1 hours.