Imagine you are working on a team and one of your team membe

Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystringfunctions, and have two files: a source file mystringfunctions.c and a header file mystringfunctions.h. In this problem, you will write automated tests known as unit tests. This program has been partially written for you in testmystringfunctions.c. Write the body of functions that are marked with a comment that begins with

Do not modify other parts of the code.

the source and header files are here for reference:

<>

#include

#include

#include

#include \"mystringfunctions.h\"

// Creates a new string from the first n chars of src

//

// Example:

// The result of deepCopyStr(\"great googly moogly\", 6) is \"great \"

char* deepCopyStr(char* src, int n) {

return NULL;

// Error if a negative integer is passed

if (n < 0) {

return NULL;

}

// Error if no src string passed

if (src == NULL) {

return NULL;

}

char* result = (char*) malloc (sizeof(char) * (n + 1));

for (int i = 0; i < n; i++) {

// Error if string has less than n characters

if (src[i] == \'\\0\') {

free(result);

return NULL;

}

result[i] = src[i];

}

result[n] = \'\\0\';

result[0] = \'a\';

return result;

}

// Checks that the first n chars of src are lowercase letters or digits

//

// Example:

// The result of isLowerOrDigitStr(\"100timeS!\", 7) is true

// The result of isLowerOrDigitStr(\"100timeS!\", 8) is false

bool isLowerOrDigitStr(char* src, unsigned int n) {

for (int i = 0; i < n; i++) {

if ((\'a\' <= src[i] && src[i] <= \'z\') ||

(\'0\' <= src[i] && src[i] <= \'9\')) {

continue;

} else {

return false;

}

}

return true;

}

// Creates a new string from the first n chars of the

// concatenation of str1 and str2

//

// Example:

// The result of deepCopyStr(\"great\", \" googly moogly\", 10) is \"great goog\"

char* concatStrs(char* str1, char* str2, int n) {

// Error if a negative integer is passed

if (n < 0) {

return NULL;

}

int j = 0;

char* result = (char*) malloc (sizeof(char) * (n + 1));

char* srcStr = str1;

for (int i = 0; i < n; i++, j++) {

// Swap to str2 if at end of str1

if (srcStr == str1 && srcStr[j] == \'\\0\') {

srcStr = str2;

j = 0;

}

// Error if strings have less than n characters

if (srcStr == str2 && srcStr[j] == \'\\0\') {

free(result);

return NULL;

}

result[i] = srcStr[j];

}

result[n] = \'\\0\';

return result;

}

<>

#ifndef MYSTRINGFUNCTIONS

#define MYSTRINGFUNCTIONS

#include

// Creates a new string from the first n chars of src

//

// Example:

// The result of deepCopyStr(\"great googly moogly\", 6) is \"great \"

char* deepCopyStr(char* src, int n);

// Checks that the first n chars of src are lowercase letters or digits

//

// Example:

// The result of isLowerOrDigitStr(\"100timeS!\", 7) is true

// The result of isLowerOrDigitStr(\"100timeS!\", 8) is false

bool isLowerOrDigitStr(char* src, unsigned int n);

// Creates a new string from the first n chars of the

// concatenation of str1 and str2

//

// Example:

// The result of deepCopyStr(\"great\", \" googly moogly\", 10) is \"great goog\"

char* concatStrs(char* str1, char* str2, int n);

#endif


<< testmystringfunctions.c >>

#include

#include

#include

#include

// Homework TODO: Include mystringfunctions.h here

// Homework TODO: Add function prototypes here

// Asks user to pick a unit test to run.

// In practice, we write unit tests using a unit testing framework

// and all unit tests are run when the code is compiled.

// For grading purposes, we ask the user to pick the test.

// DO NOT modify this function for your homework.

int main() {

// User menu

printf(\"Which unit test would you like to run?\ \");

printf(\"1) deepCopyStr\ \");

printf(\"\\ta) n = 2, src = \\\"test string\\\"\ \");

printf(\"\\tb) n = 0 returns \\\"\\\\0\\\"\ \");

printf(\"\\tc) negative n returns NULL\ \");

printf(\"2) isLowerOrDigitStr\ \");

printf(\"\\ta) n = 4, src = \\\"testString\\\"\ \");

printf(\"\\tb) n = 5, src = \\\"testString\\\"\ \");

printf(\"\\tc) n = 0\ \");

printf(\"3) concatStrs\ \");

printf(\"\\ta) n = 5, str1 = \\\"test\\\", str2 = \\\"string\\\"\ \");

printf(\"\\tb) n = 5, str1 = \\\"\\\", str2 = \\\"test string\\\" returns \\\"test \\\"\ \");

printf(\"\\tc) n = 5, str1 = \\\"test\\\", str2 = \\\"\\\" returns NULL\ \");

while (!getAndRunTest()) {}

}

// Prompt user once for the test to run. 1a is deepCopyStrTestA,

// 1b is deepCopyStrTestB, 2c is isLowerOrDigitStrTestC, and so on.

// If the user input is valid, run the test and return true.

// If the user input is invalid, print the error according to the homework

// prompt and return false.

bool getAndRunTest() {

// Homework TODO: complete the code for this function.

}

// Test n = 2 and src = \"test string\" returns \"te\"

void deepCopyStrTestA() {

char* result = deepCopyStr(\"test string\", 2);

// if (result) checks to see something is returned (even if the string is empty).

// We will see later in the course this is checking if the result is a NULL pointer

assert(result && result[0] == \'t\' && result[1] == \'e\' && result[2] == \'\\0\');

printf(\"Test successful.\ \");

}

// Test n = 0 the returns \"\\0\"

void deepCopyStrTestB() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Test negative n returns NULL\"

void deepCopyStrTestC() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Test n = 4, src = \"testString\" returns true

void isLowerOrDigitStrTestA() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Test n = 5, src = \"testString\" returns false

void isLowerOrDigitStrTestB() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Test n = 0, src = \"\" returns true

void isLowerOrDigitStrTestC() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Test n = 10, str1 = \"test\", str2 = \"string\" returns \"teststring\"

void concatStrsTestA() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// n = 5, str1 = \"\", str2 = \"test string\" returns \"test \"

void concatStrsTestB() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// n = 5, str1 = \"test\", str2 = \"\" returns NULL

void concatStrsTestC() {

// Homework TODO: write code for this test case according to the

// specifications in the comment above the function.

}

// Flush stdin buffer

void flushStdin() {

// Homework TODO: see 1/30/17 lecture notes to understand what this

// function will do and how it should be written. (do not worry about this part, the answer to this has been provided);

char c;

//skip all characters until end-of-file marker

// or new line/carriage return

while ( ( c = getchar( ) ) != EOF && c != \'\ \' && c != \'\ \' ) {};

}

Solution

Solution:

1. mystringfunctions.h: See the code below

------------------------------------------

#ifndef MYSTRINGFUNCTIONS_H_
#define MYSTRINGFUNCTIONS_H_

#include <stdio.h>

// Creates a new string from the first n chars of src
//
// Example:
// The result of deepCopyStr(\"great googly moogly\", 6) is \"great \"
char* deepCopyStr(char* src, int n);

// Checks that the first n chars of src are lowercase letters or digits
//
// Example:
// The result of isLowerOrDigitStr(\"100timeS!\", 7) is true
// The result of isLowerOrDigitStr(\"100timeS!\", 8) is false
int isLowerOrDigitStr(char* src, unsigned int n);

// Creates a new string from the first n chars of the
// concatenation of str1 and str2
//
// Example:
// The result of deepCopyStr(\"great\", \" googly moogly\", 10) is \"great goog\"
char* concatStrs(char* str1, char* str2, int n);

#endif /* MYSTRINGFUNCTIONS_H_ */

--------------------------------------------

2. mystringfunctions.c: See the code below:

-----------------------------------------

#include \"mystringfunctions.h\"

// Creates a new string from the first n chars of src
//
// Example:
// The result of deepCopyStr(\"great googly moogly\", 6) is \"great \"

char* deepCopyStr(char* src, int n) {
   //return NULL;
   // Error if a negative integer is passed
   if (n < 0) {
       return NULL;
   }

   // Error if no src string passed
   if (src == NULL) {
       return NULL;
   }

   char* result = (char*) malloc(sizeof(char) * (n + 1));

   for (int i = 0; i < n; i++) {
       // Error if string has less than n characters
       if (src[i] == \'\\0\') {
           free(result);
           return NULL;
       }
       result[i] = src[i];
   }
   result[n] = \'\\0\';
   result[0] = \'a\';
   return result;
}

// Checks that the first n chars of src are lowercase letters or digits
//
// Example:
// The result of isLowerOrDigitStr(\"100timeS!\", 7) is true
// The result of isLowerOrDigitStr(\"100timeS!\", 8) is false
int isLowerOrDigitStr(char* src, unsigned int n) {
   for (int i = 0; i < n; i++) {
       if ((\'a\' <= src[i] && src[i] <= \'z\') || (\'0\' <= src[i] && src[i] <= \'9\')) {
           continue;
       } else {
           return 0;
       }
   }
   return 1;
}

// Creates a new string from the first n chars of the
// concatenation of str1 and str2
//
// Example:
// The result of deepCopyStr(\"great\", \" googly moogly\", 10) is \"great goog\"
char* concatStrs(char* str1, char* str2, int n) {
   // Error if a negative integer is passed
   if (n < 0) {
       return NULL;
   }

   int j = 0;

   char* result = (char*) malloc(sizeof(char) * (n + 1));
   char* srcStr = str1;

   for (int i = 0; i < n; i++, j++) {

       // Swap to str2 if at end of str1
       if (srcStr == str1 && srcStr[j] == \'\\0\') {
           srcStr = str2;
           j = 0;
       }

       // Error if strings have less than n characters
       if (srcStr == str2 && srcStr[j] == \'\\0\') {
           free(result);
           return NULL;
       }

       result[i] = srcStr[j];
   }
   result[n] = \'\\0\';
   return result;
}

---------------------------------

3. testmystringfunctions.c: See the code below:

---------------------------------

#include <stdlib.h>
#include <assert.h>

// Homework TODO: Include mystringfunctions.h here
#include \"mystringfunctions.h\"

// Homework TODO: Add function prototypes here
int getAndRunTest();
void deepCopyStrTestA();
void deepCopyStrTestB();
void deepCopyStrTestC();
void isLowerOrDigitStrTestA();
void isLowerOrDigitStrTestB();
void isLowerOrDigitStrTestC();
void concatStrsTestA();
void concatStrsTestB();
void concatStrsTestC();
void flushStdin();

// Asks user to pick a unit test to run.
// In practice, we write unit tests using a unit testing framework
// and all unit tests are run when the code is compiled.
// For grading purposes, we ask the user to pick the test.
// DO NOT modify this function for your homework.
int main() {

   // User menu
   printf(\"Which unit test would you like to run?\ \");
   printf(\"1) deepCopyStr\ \");
   printf(\"\\ta) n = 2, src = \\\"test string\\\"\ \");
   printf(\"\\tb) n = 0 returns \\\"\\\\0\\\"\ \");
   printf(\"\\tc) negative n returns NULL\ \");
   printf(\"2) isLowerOrDigitStr\ \");
   printf(\"\\ta) n = 4, src = \\\"testString\\\"\ \");
   printf(\"\\tb) n = 5, src = \\\"testString\\\"\ \");
   printf(\"\\tc) n = 0\ \");
   printf(\"3) concatStrs\ \");
   printf(\"\\ta) n = 5, str1 = \\\"test\\\", str2 = \\\"string\\\"\ \");
   printf(\"\\tb) n = 5, str1 = \\\"\\\", str2 = \\\"test string\\\" returns \\\"test \\\"\ \");
   printf(\"\\tc) n = 5, str1 = \\\"test\\\", str2 = \\\"\\\" returns NULL\ \");
   while (!getAndRunTest()) {
   }
}

// Prompt user once for the test to run. 1a is deepCopyStrTestA,
// 1b is deepCopyStrTestB, 2c is isLowerOrDigitStrTestC, and so on.
// If the user input is valid, run the test and return true.
// If the user input is invalid, print the error according to the homework
// prompt and return false.

int getAndRunTest() {
   // Homework TODO: complete the code for this function.
   int choice;
   char sub_choice;
   printf(\"Choice:\");
   fflush(stdout);
   scanf(\"%d\",&choice);
   //processing of choice
   switch(choice)
   {
   case 1:
       printf(\"Sub-choice:\");
       fflush(stdout);
       scanf(\" %c\",&sub_choice);
       switch(sub_choice)
       {
       case \'a\':
           deepCopyStrTestA();
           break;
       case \'b\':
           deepCopyStrTestB();
           break;
       case \'c\':
           deepCopyStrTestC();
           break;
       }
       break;
   case 2:
       printf(\"Sub-choice:\");
       fflush(stdout);
       scanf(\" %c\",&sub_choice);
       switch(sub_choice)
       {
       case \'a\':
           isLowerOrDigitStrTestA();
           break;
       case \'b\':
           isLowerOrDigitStrTestB();
           break;
       case \'c\':
           isLowerOrDigitStrTestC();
           break;
       }
       break;
   case 3:
       printf(\"Sub-choice:\");
       fflush(stdout);
       scanf(\" %c\",&sub_choice);
       switch(sub_choice)
       {
       case \'a\':
           concatStrsTestA();
           break;
       case \'b\':
           concatStrsTestB();
           break;
       case \'c\':
           concatStrsTestC();
           break;
       }
       break;
   default:
       fprintf(stderr,\"Wrong choice! Exiting...\");
       //return 0;
       exit(-1);
   }
   return 1;
}

// Test n = 2 and src = \"test string\" returns \"te\"
void deepCopyStrTestA() {

   char* result = deepCopyStr(\"test string\", 2);

   // if (result) checks to see something is returned (even if the string is empty).
   // We will see later in the course this is checking if the result is a NULL pointer

   assert(result && result[0] == \'t\' && result[1] == \'e\' && result[2] == \'\\0\');

   printf(\"Test successful.\ \");

}

// Test n = 0 the returns \"\\0\"
void deepCopyStrTestB() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   char* result = deepCopyStr(\"test string\", 0);

   assert(result[0] == \'\\0\');

   printf(\"Test successful.\ \");

}

// Test negative n returns NULL\"

void deepCopyStrTestC() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   char* result = deepCopyStr(\"test string\", -2);

   assert(result == NULL);

   printf(\"Test successful.\ \");

}

// Test n = 4, src = \"testString\" returns true
void isLowerOrDigitStrTestA() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   int result = isLowerOrDigitStr(\"testString\", 4);

   assert(result == 1);

   printf(\"Test successful.\ \");
}

// Test n = 5, src = \"testString\" returns false
void isLowerOrDigitStrTestB() {
   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   int result = isLowerOrDigitStr(\"testString\", 5);

   assert(result == 0);

   printf(\"Test successful.\ \");

}

// Test n = 0, src = \"\" returns true
void isLowerOrDigitStrTestC() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   int result = isLowerOrDigitStr(\"\", 0);

   assert(result == 1);

   printf(\"Test successful.\ \");

}

// Test n = 10, str1 = \"test\", str2 = \"string\" returns \"teststring\"
void concatStrsTestA() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   char* result=concatStrs(\"test\",\"string\",10);

   assert(result && result[0] == \'t\' && result[1] == \'e\' && result[2] == \'s\' && result[3] == \'t\' &&
           result[4] == \'s\' && result[5] == \'t\' && result[6] == \'r\' && result[7] == \'i\' && result[8] == \'n\' &&
           result[9] == \'g\' && result[10] == \'\\0\');

   printf(\"Test successful.\ \");

}

// n = 5, str1 = \"\", str2 = \"test string\" returns \"test \"
void concatStrsTestB() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   char* result=concatStrs(\"\",\"test\",5);

   assert(result && result[0] == \'t\' && result[1] == \'e\' && result[2] == \'s\' && result[3] == \'t\' && result[4] == \'\\0\');

   printf(\"Test successful.\ \");

}

// n = 5, str1 = \"test\", str2 = \"\" returns NULL
void concatStrsTestC() {

   // Homework TODO: write code for this test case according to the
   // specifications in the comment above the function.
   char* result=concatStrs(\"test\",\"\",5);

   assert(result == NULL);

   printf(\"Test successful.\ \");
}

// Flush stdin buffer
void flushStdin() {

   // Homework TODO: see 1/30/17 lecture notes to understand what this
   //<Not given>
   // function will do and how it should be written. (do not worry about this part, the answer to this has been provided);

   char c;
   //skip all characters until end-of-file marker
   // or new line/carriage return

   while ((c = getchar()) != EOF && c != \'\ \' && c != \'\ \') {
   };
}

--------------------------------

Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site