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

Imagine you are working on a team and one of your team members is writing a libr

ID: 3793266 • Letter: I

Question

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] == '') {

free(result);

return NULL;

}

result[i] = src[i];

}

result[n] = '';

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] == '') {

srcStr = str2;

j = 0;

}

// Error if strings have less than n characters

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

free(result);

return NULL;

}

result[i] = srcStr[j];

}

result[n] = '';

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(" a) n = 2, src="test string" ");

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

printf(" c) negative n returns NULL ");

printf("2) isLowerOrDigitStr ");

printf(" a) n = 4, src="testString" ");

printf(" b) n = 5, src="testString" ");

printf(" c) n = 0 ");

printf("3) concatStrs ");

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

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

printf(" c) 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] == '');

printf("Test successful. ");

}

// Test n = 0 the returns ""

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 != ' ' ) {};

}

attached are also example outputs the first one is if mystringfunctions.c is correct. the second is when it is incorrect.

correct:

4

incorrect:

Explanation / Answer


#include <stdbool.h>
#ifndef NULL
#endif
typedef int bool;
#define true 1
#define false 0
#include "mystringfunctions.h"
bool getAndRunTest();
void deepCopyStrTestA();
void deepCopyStrTestB();
void deepCopyStrTestC();
void isLowerOrDigitStrTestA();
void isLowerOrDigitStrTestB();
void isLowerOrDigitStrTestC();
void concatStrsTestA();
void concatStrsTestB();
void concatStrsTestC();
// 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(" a) n = 2, src="test string" ");
printf(" b) n = 0 returns "\0" ");
printf(" c) negative n returns NULL ");
printf("2) isLowerOrDigitStr ");
printf(" a) n = 4, src="testString" ");
printf(" b) n = 5, src="testString" ");
printf(" c) n = 0 ");
printf("3) concatStrs ");
printf(" a) n = 5, str1 = "test", str2 = "string" ");
printf(" b) n = 5, str1 = "", str2 = "test string" returns "test " ");
printf(" c) 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() {
char input;
char testcase;
printf("Enter 1, 2, or 3 for the function to test.");
scanf("%s",input);

if (input == '1')
{
   printf("Enter a, b, or c for the test case.");
   scanf("%s",testcase);
   if(testcase == 'a')
   {
       deepCopyStrTestA();
   }
   else if(testcase == 'b')
   {
       deepCopyStrTestB();
   }
   else if(testcase == 'c')
   {
       deepCopyStrTestC();
   }

}
else if (input == '2')
{
   printf("Enter a, b, or c for the test case.");
   scanf("%s",testcase);
   if(testcase == 'a')
       {
           isLowerOrDigitStrTestA();
       }
       else if(testcase == 'b')
       {
           isLowerOrDigitStrTestB();
       }
       else if(testcase == 'c')
       {
           isLowerOrDigitStrTestC();
       }

}
else if(input == '3'){

   printf("Enter a, b, or c for the test case.");
   scanf("%s",testcase);
       if(testcase == 'a')
       {
           concatStrsTestA();
       }
       else if(testcase == 'b')
       {
           concatStrsTestB();
       }
       else if(testcase == 'c')
       {
           concatStrsTestC();
       }


}
else{
   printf("Enter 1, 2, or 3 for the function to test.");
   scanf("%s",input);
}


}
// 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] == '');
printf("Test successful. ");
}
// Test n = 0 the returns ""
void deepCopyStrTestB() {
   char* result = deepCopyStr("test string", 0);
   assert(result && result[2] == '');
   printf("Test successful. ");
}
// Test negative n returns NULL"
void deepCopyStrTestC() {
   char* result = deepCopyStr("test string", -2);
   assert(result == NULL);
   printf("Test successful. ");
}
// Test n = 4, src="testString" returns true
void isLowerOrDigitStrTestA() {
   bool result=isLowerOrDigitStr("testString",4);
   assert(result);
   printf("Test successful. ");
}
// Test n = 5, src="testString" returns false
void isLowerOrDigitStrTestB() {
   bool result=isLowerOrDigitStr("testString",5);
   assert(!bool);
   printf("Test successful. ");
}
// Test n = 0, src="" returns true
void isLowerOrDigitStrTestC() {
   bool result=isLowerOrDigitStr("",0);
   assert(result);
   printf("Test successful. ");
}
// Test n = 10, str1 = "test", str2 = "string" returns "teststring"
void concatStrsTestA() {
   char* result = concatStrs("test","string",10);
   assert(result && result[2] == 'teststring');
   printf("Test successful. ");
}
// n = 5, str1 = "", str2 = "test string" returns "test "
void concatStrsTestB() {
   char* result = concatStrs("","test string",5);
   assert(result && result[2] == 'test');
   printf("Test successful. ");
}
// n = 5, str1 = "test", str2 = "" returns NULL
void concatStrsTestC() {
   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
// 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 != ' ' ) {};
}

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