Answered step by step
Verified Expert Solution
Question
1 Approved Answer
must program in C - any help Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves. The
must program in C - any help
Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves. The five functions to implement are streat, strstr, strchr, strdup, and streg. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement cach of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! 64 int main(int argc, char** argv) t 65 Read strings from program arguments * if(argc != 3) { printf ("usage: ./strfuncs s1 s2 ) return 1: 6B char* s1- argv[11: char* s2argvL21; printf("String 1: %s ", s1); printf("String 2: %s ", s2); 70 Check for string equality/ int s1eqs2 = streg(s1, s2); printf("s1-s2? %s .' s1eqs2 ? 75 "yes" : "no"); 7B / Concatenate s1 to s2 and s2 char* s1s2 strcat (s1, s2); char* s2s1 = strcat (s2, s1); printf("s1+s2-%s ", s1s2); printf("s2+s1-%s ", s2s1); to s1* 80 /Check for substrings */ char* siins2 = strstr(s2, s1); char* s2ins1- strs tr(s1, s2); printf( "s in s2-> %s ", s1ins2 == NULL ? printf("s2 in s1 %s ", s2ins1-NULL 85 "no" ? "no" "yes"); "yes"); 90 Check for character occurence char* ains1 str_chr(s1, 'a') print f(".a. in s1? %s ", ains1_NULL ? "no" : "yes"); /Check duplication of strings char* dups1 = strdup(s1); printf("dup (s1)=%s ", dups1); 95 */ /* clean up, 1.e. free memory! */ //Add your code here 100 101 102 103 /* Done!* return e char* str_chr(char s[l, char c) //Add your code here /*Returns a pointer to a new string which is a copy of the given string s.*/ char strdup(char s[]) f //Add your code here /** Returns 1 if the strings s1 and s2 are the same, returns otherwise.* int streq(char s1[], char s2[]) //Add your code here /*x Main function. Add code to free allocated memory * Valgrind should NOT yield any errors once you're done! DO NOT CHANGE OUTPUTS! JUST ADD CLEAN UP CODE! int lenstr(char* s) int len = 0; while(s[len++]0') return len-1; /*Appends the src string to the end of the * dest string. Return the resulting string.*/ char* strcat(char dest[l, char src[]) //Add your code here /** Searches the haystack string for the needle substring. Return a pointer to the located *needle substring in the haystack string if it exists (and the first if there are more than one), or NULL if the needle is not in * the haystack. char* strstr(char haystack[], char needle[]) //Add your code here k Searches for the first occurrence of the * character c in the string s and returns a * pointer to it. If c does not appear in s return NULL* Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves. The five functions to implement are streat, strstr, strchr, strdup, and streg. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement cach of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! 64 int main(int argc, char** argv) t 65 Read strings from program arguments * if(argc != 3) { printf ("usage: ./strfuncs s1 s2 ) return 1: 6B char* s1- argv[11: char* s2argvL21; printf("String 1: %s ", s1); printf("String 2: %s ", s2); 70 Check for string equality/ int s1eqs2 = streg(s1, s2); printf("s1-s2? %s .' s1eqs2 ? 75 "yes" : "no"); 7B / Concatenate s1 to s2 and s2 char* s1s2 strcat (s1, s2); char* s2s1 = strcat (s2, s1); printf("s1+s2-%s ", s1s2); printf("s2+s1-%s ", s2s1); to s1* 80 /Check for substrings */ char* siins2 = strstr(s2, s1); char* s2ins1- strs tr(s1, s2); printf( "s in s2-> %s ", s1ins2 == NULL ? printf("s2 in s1 %s ", s2ins1-NULL 85 "no" ? "no" "yes"); "yes"); 90 Check for character occurence char* ains1 str_chr(s1, 'a') print f(".a. in s1? %s ", ains1_NULL ? "no" : "yes"); /Check duplication of strings char* dups1 = strdup(s1); printf("dup (s1)=%s ", dups1); 95 */ /* clean up, 1.e. free memory! */ //Add your code here 100 101 102 103 /* Done!* return e char* str_chr(char s[l, char c) //Add your code here /*Returns a pointer to a new string which is a copy of the given string s.*/ char strdup(char s[]) f //Add your code here /** Returns 1 if the strings s1 and s2 are the same, returns otherwise.* int streq(char s1[], char s2[]) //Add your code here /*x Main function. Add code to free allocated memory * Valgrind should NOT yield any errors once you're done! DO NOT CHANGE OUTPUTS! JUST ADD CLEAN UP CODE! int lenstr(char* s) int len = 0; while(s[len++]0') return len-1; /*Appends the src string to the end of the * dest string. Return the resulting string.*/ char* strcat(char dest[l, char src[]) //Add your code here /** Searches the haystack string for the needle substring. Return a pointer to the located *needle substring in the haystack string if it exists (and the first if there are more than one), or NULL if the needle is not in * the haystack. char* strstr(char haystack[], char needle[]) //Add your code here k Searches for the first occurrence of the * character c in the string s and returns a * pointer to it. If c does not appear in s return NULL*Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started