Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Purpose of this assignment is to write out all of the string functions in C

ID: 3587524 • Letter: T

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 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;

}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

/** 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

char *ptr = dest;

while(*ptr != '') //iterate till end of 1st string

ptr++;

while(*ptr++ = *src++); //it will copy src string char by char

printf("**** in strcat by pointer = %s ",*dest);

return dest;

}

char* strcat_arr(char dest[], char src[]) {

// TODO implement me using array syntax!

int loop1 = 0, loop2 = 0;

while(dest[loop1] != '') //iterate till end of 1st string

loop1++;

while(dest[loop1++] = src[loop2++]); //add 2nd string at the end of 1st

printf("**** in strcat by array = %s ",*dest);

return dest;

}

/** 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

if (!haystack || !needle)

{

return NULL;

}

while(*haystack != '')

{

int len = 0;

const char* sub = needle;

while(*sub != '')

{

if (*sub == *haystack)

{

sub++;

haystack++;

len++;

if (*sub == '')

return haystack - len;

}

else

{

haystack -= len;

break;

}

}

haystack++;

}

return NULL;

}

char* strstr_arr(char haystack[], char needle[]) {

// TODO implement me using array syntax!

int loop1, loop2;

for(loop1 =0; haystack[loop1] != ''; loop1++)

{

if(haystack[loop1] == needle[0])

{

for(loop2 =1; needle[loop2] != ''; loop2++)

{

if(needle[loop2] != haystack[loop1+loop2])

break;

if(needle[loop2] == '')

return (haystack+loop1);

}

}

}

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!

while(*s)

{

if(*s == c)

break;

s++;

}

if(*s == '')

return NULL;

else

return s;

}

char* strchr_arr(char s[], char c) {

// TODO implement strchr using array syntax!

int loop = 0;

for(loop = 0; s[loop] != '' ; loop++)

{

if(s[loop] == c)

return (s+loop);

}   

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!

char *ptr = NULL;

while((*ptr = *s) != '')

{

ptr++;

s++;

}

return ptr;

}

char* strdup_arr(char s[]) {

// TODO implement strdup using array syntax!

char *ptr = NULL;

int loop = 0;

while(ptr[loop] = s[loop]);

return ptr;

}

/** 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!

while(*s1 == *s2)

{

if(*s1 == '')

return 1;   

s1++;

s2++;

}

return 0;

}

int streq_arr(char s1[], char s2[]) {

// TODO implement streq using array syntax!

int loop = 0;

for(loop =0; s1[loop] == s2 [loop]; loop++)

if(s1[loop] == '')

return 1; //1 means string are same

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");

printf("********* ");

/* 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);

printf("after cat command ******** ");

/* 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! */

  free(s1);
free(s2);
free(s1s2ptr);
free(s2s1ptr);
free(s1s2arr);
free(s2s1arr);

// TODO implement code to clean up!

/* Done! */

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote