Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// File is str_cmp_main.c /* The file is a test file for the string comparison functions */ #include stdio.h #include stdlib.h #include string.h #include time.h

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

// File is str_cmp_main.c /* The file is a test file for the string comparison functions */

#include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h"

int myStrCmp(char *s1, char *s2);

int main(int argc, char* argv[]) {

char *s1 = "Monday"; char *s2 = "Monster day";

printf("Answer should be -1 myStrCmp= %d ", myStrCmp(s1, s2)); printf("Answer should be 1 myStrCmp= %d ", myStrCmp(s2, s1));

// note that here the program passess subsections of the original strings as // strings to the comparison function printf("Answer should be 0 myStrCmp= %d ", myStrCmp(&s1[3], &s2[8]));

return 0; }

Purpose: gaining experience with: a. working with string pointers, b. taking advantage of "call by value" this part of the tutorial you will code a recursive function, myStrCmpl), for comparing two strings Create two files mystr.c and mystr.h, which will contain the code and the function prototype respectively. Use the file str_cmp_main.c to test your code Input: Input consists of two strings s1 and s2 (given as address to the memory location) In this part of the tutorial you will code a recursive function, myStrCmp(), for comparing two strings Output: None Return: -1 if string s1 should appear before string s2 in lexicographic order. 0 if string s1 is the same as s2 1 is s2 appears before s1 in lexicographic order. Assumption: The value of s1 and/or s2 is not NULL For example: if s1- "Monday" and s2 "Monster" then a call to myStrCmp(s1, s2) would return -1 because Monday should appear before Sunday in a lexicographic order. S1 M 200 S2 M 230 Al Algorithm layout Compare the characters of the two strings one at a time until either one of the strings is empty (all characters were exhausted), or the two characters are different. Compare the last two characters that were tested and return the result of the comparison In this function we take advantage of the fact that all parameters passed to the function are "called by value". Namely, the parameters are a local copy. This means that we can change the value of the pointer (namely, the address stored in the pointer without external impact) Comparing the current characters pointed to by s1 and s2 is done by using the* operator. For example the statement if (*s1--*s2) {"do something") compares the characters for equality. In the example above *s1 is the first character at position s1[0] which is 'M'. Checking whether s1 is an empty string is carried out by comparing the character pointed to by s1 to the character 0' (the sentinel). For example if (*s1 !'O') {"do something" } or if (*S1 !:0) {"do something") In order to advance the addresses stored in the pointers to point to the next character one can use the ++'operator (e.g., advancing the pointer s1 using s1++ will change the content of s1 to 201 and it will point to the letter 'O') Function prototype int myStrCmp(char *s1, char *s2); Pseudo code +code Int myStrCmp(char *s1, char *s2) // recursion condition // if (s1 is not empty and s2 is not empty and the characters pointed to by s1 and s2 are the same) then recurs by advancing s1 and s2 to point to the next character return(myStrCmp(s1++, s2++)); // one of the strings may be empty or they contain different characters // return -1 if *s1 precedes *s2 in the lexicographic order if (*s1 s2) Code the function and test it. Most likely you will get a segmentation violation. Using the debugger try to understand why you receive segmentation violation and fix the function. Take 5 minutes to work on the problem. Did you identify the problem? The problem is that the recursive call to myStrCmp) with the parameters s1++, and s2++ is not correct. The postfix operator++advances the pointers s1 and s2 only after the call to myStrCmp is complete. Here however we want the pointers s1 and s2 to advance to the next location before the recursive call to myStrCmp() as shown in the figure below. MONSTER MONDAY Solution: There are two options of achieving it: a. Advance each pointer independently and then call myStrCmp s1++ S2++ myStrCmp(s1, s2); b. Use the prefix operator + to do so. Namely call myStrCmp(++s1, ++s2)

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions