Question
The Purpose of this assignment is to write out all of the string functions in C without using any of the built in functions. Please
The Purpose of this assignment is to write out all of the string functions in C without using any of the built in functions. Please use malloc and free to allocate/deallocate as necessary.
Here is the code structure/ methods that need to be written:
#include
/** Appends the src string to the end of the
* dest string. Return the resulting string. */
char* strcat_ptr(char* dest, char* src) {
// TODO implement me using pointer syntax
return NULL;
}
char* strcat_arr(char dest[], char src[]) {
// TODO implement me using array syntax!
return NULL;
}
/** 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_ptr(char* haystack, char* needle) {
// TODO implement me using pointer syntax
return NULL;
}
char* strstr_arr(char haystack[], char needle[]) {
// TODO implement me using array syntax!
return NULL;
}
/** 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. */
char* strchr_ptr(char *s, char c) {
// TODO implement strchr using pointer syntax!
return NULL;
}
char* strchr_arr(char s[], char c) {
// TODO implement strchr using array syntax!
return NULL;
}
/** Returns a pointer to a new string which is
* a copy of the given string s. */
char* strdup_ptr(char* s) {
// TODO implement strdup using pointer syntax!
return NULL;
}
char* strdup_arr(char s[]) {
// TODO implement strdup using array syntax!
return NULL;
}
/** Returns 1 if the strings s1 and s2 are the
* same, returns 0 otherwise. */
int streq_ptr(char* s1, char* s2) {
// TODO implement streq using pointer syntax!
return 0;
}
int streq_arr(char s1[], char s2[]) {
// TODO implement streq using array syntax!
return 0;
}
/** 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 main(int argc, char** argv) {
/* Read strings from program arguments */
if(argc != 3) {
printf("usage: ./strfuncs s1 s2 ");
return 1;
}
char* s1 = argv[1];
char* s2 = argv[2];
printf("String 1: %s ", s1);
printf("String 2: %s ", s2);
/* Check for string equality */
int s1eqs2ptr = streq_ptr(s1, s2);
int s1eqs2arr = streq_arr(s1, s2);
printf("ptr: s1=s2? %s ", s1eqs2ptr ? "yes" : "no");
printf("arr: s1=s2? %s ", s1eqs2arr ? "yes" : "no");
/* Concatenate s1 to s2 and s2 to s1 */
char* s1s2ptr = strcat_ptr(s1, s2);
char* s2s1ptr = strcat_ptr(s2, s1);
char* s1s2arr = strcat_arr(s1, s2);
char* s2s1arr = strcat_arr(s2, s1);
printf("ptr: s1+s2=%s ", s1s2ptr);
printf("ptr: s2+s1=%s ", s2s1ptr);
printf("arr: s1+s2=%s ", s1s2arr);
printf("arr: s2+s1=%s ", s2s1arr);
/* Check for substrings */
char* s1ins2ptr = strstr_ptr(s2, s1);
char* s2ins1ptr = strstr_ptr(s1, s2);
char* s1ins2arr = strstr_arr(s2, s1);
char* s2ins1arr = strstr_arr(s1, s2);
printf("ptr: s1 in s2 -> %s ", s1ins2ptr == NULL ? "no" : s1ins2ptr);
printf("ptr: s2 in s2 -> %s ", s2ins1ptr == NULL ? "no" : s2ins1ptr);
printf("arr: s1 in s2 -> %s ", s1ins2arr == NULL ? "no" : s1ins2arr);
printf("arr: s2 in s1 -> %s ", s2ins1arr == NULL ? "no" : s2ins1arr);
/* Check for character occurence */
char* ains1ptr = strchr_ptr(s1, 'a');
char* ains1arr = strchr_arr(s1, 'a');
printf("ptr: 'a' in s1? %s ", ains1ptr == NULL ? "no" : ains1ptr);
printf("arr: 'a' in s1? %s ", ains1arr == NULL ? "no" : ains1arr);
/* Check duplication of strings */
char* dups1ptr = strdup_ptr(s1);
char* dups1arr = strdup_arr(s1);
printf("ptr: dup(s1)=%s ", dups1ptr);
printf("arr: dup(s1)=%s ", dups1arr);
/* Clean up, i.e. free memory! */
// TODO implement code to clean up!
/* Done! */
return 0;
}
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