Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include /** Appends the src string to the end of the * dest string. Return the resulting string. */ char* strcat_ptr(char* dest, char*

#include

#include

#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!

char *rdest = dest;

while(*dest)

dest++;

while(*dest++ = *src++);

return rdest;

}

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

{

// TODO implement me using array syntax!

int i,j;

for (i = 0; dest[i] != '\0'; i++) ;

for (j = 0; src[j] != '\0'; j++)

dest[i+j] = src[j];

dest[i+j] = '\0';

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

while(*haystack)

{

char *Begin = haystack;

char *pattern = needle;

while(*haystack && *pattern && *haystack == *pattern)

{

haystack++;

pattern++;

if(!*pattern)

return Begin;

haystack = Begin + 1;

}

}

return NULL; //return NULL might be outside of the function

}

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!

while((*s != c) && *s)

s++;

return (s);

}

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!

char *d = malloc(strlen(s) + 1);

if(d == NULL) return NULL;

strcpy(d,s);

return d;

}

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 (*s1 == *s2 && *s2 =='\0')? 1: (*s1 == *s2)? streq_ptr(++s1, ++s2): 0;

}

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

// TODO implement streq using array syntax!

int i = 0, j = 0;

while(s1[i] != '\0' && s2[j] != '\0')

{

if(s1[i] != s2[j])

return 0;

i++;

j++;

}

if(i != j) return 0;

else if (s1[i] != '\0' || s2[i] != '\0') return 0;

return 1;

}

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

}

FIX ALL THE ERRORS IN THIS CODE AND IMPLEMENT MISSING FUNCTIONS,INCLUDING MALLOC AND FREE IMPLEMENTATION

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How to prepare washing soda from common salt?

Answered: 1 week ago

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago