Question

In: Computer Science

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

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

// Homework TODO: ...

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?\n");

printf("1) deepCopyStr\n");

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

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

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

printf("2) isLowerOrDigitStr\n");

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

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

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

printf("3) concatStrs\n");

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

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

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

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.\n");

}

// 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 != '\n' && c != '\r' ) {};

}

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

correct:

Which unit test would you like to run?
1) deepCopyStr
a) n = 2, src = "test string"
b) n = 0 returns "\0"
c) negative n returns NULL
2) isLowerOrDigitStr
a) n = 4, src = "testString"
b) n = 5, src = "testString"
c) n = 0
3) concatStrs
a) n = 5, str1 = "test", str2 = "string"
b) n = 5, str1 = "", str2 = "test string" returns "test "
c) n = 5, str1 = "test", str2 = "" returns NULL
pf
Enter 1, 2, or 3 for the function to test.
p
f
Enter 1, 2, or 3 for the function to test.
1

4

l
Enter a, b, or c for the test case.
1a
Test successful.

incorrect:

Which unit test would you like to run?
1) deepCopyStr
a) n = 2, src = "test string"
b) n = 0 returns "\0"
c) negative n returns NULL
2) isLowerOrDigitStr
a) n = 4, src = "testString"
b) n = 5, src = "testString"
c) n = 0
3) concatStrs
a) n = 5, str1 = "test", str2 = "string"
b) n = 5, str1 = "", str2 = "test string" returns "test "
c) n = 5, str1 = "test", str2 = "" returns NULL
1a
Assertion failed: (result && result[0] == ’t’ && result[1] == ’e’ && result[2] == ’\0’), failed 
Abort trap: 6

Solutions

Expert Solution

//mustringfunction.c

//-------------------------------------------------------------------------------------------
#include <stdio.h>
//#include"mystringfunctions.h"
#include<malloc.h>
#include<stdbool.h>
#include<assert.h>

char* deepCopyStr(char* src, int n)
{
if (n < 0) {
return NULL;
}
// Error if no src string passed
if (src == NULL) {
return NULL;
}
char* result = (char*) malloc (sizeof(char) * (n + 1));
int i;
for (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;
}
bool isLowerOrDigitStr(char* src, unsigned int n)
{
int i;
for (i = 0; i < n; i++) {
if (('a' <= src[i] && src[i] <= 'z') || ('0' <= src[i] && src[i] <= '9')) {
continue;
}
else {
return false;
}
}
return true;
}
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;
int i;
for (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;
}

// 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.\n");
}
// 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);
printf("Test successful.\n");
}
// 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);
printf("Test successful.\n");
}
// 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.
bool b = isLowerOrDigitStr("test string",4);
assert(b);
printf("Test successful.\n");
}
// 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.
bool b = isLowerOrDigitStr("test string",5);
assert(b);
printf("Test successful.\n");

}
// 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.
bool b = isLowerOrDigitStr("",0);
assert(b);
printf("Test successful.\n");
}
// 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");
}
// 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 string",5);
assert(result && result[0] == 't' && result[1] == 'e' && result[2] == 's' && result[3] == 't' && result[4]==' ' && result[5]=='\0');
printf("Test successful.\n");
}
// 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);
printf("Test successful.\n");
}
// 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 != '\n' && c != '\r' ) {};
}
// 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.
char ch1,ch2;
printf("Enter 1, 2, or 3 for the function to test.\n");
scanf("%c",&ch1);
  
  
if(ch1 == '1')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestC();}
else
return false;
}
else if(ch1 == '2')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c,",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestC();}
else
return false;   
}
else if(ch1 == '3')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c,",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); concatStrsTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); concatStrsTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); concatStrsTestC();}
else
return false;
}
else
{
return false;
}
return true;
  
}

//---------------------------------------------------------------------------------------------
//mystringfunction.h

#ifndef MYSTRINGFUNCTIONS_H
#define MYSTRINGFUNCTIONS_H
#include<stdio.h>
#include<stdbool.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
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

//-------------------------------------------------------------------------------

//testmystringfunction.c

#include "mystringfunctions.h"
#include <stdio.h>
#include<stdbool.h>

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

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


//-----------------------------------------------------------------------------------------------------

Output:


Related Solutions

1. You are the head of a research team, and you are training your team members...
1. You are the head of a research team, and you are training your team members on the index number technique. Your task is to explain to your team members that when measuring an overall price increase (e.g., inflation), the Laspeyres index likely overestimates while the Paasche index likely underestimates it.
You are a project team manager, and your team members report each day to you to...
You are a project team manager, and your team members report each day to you to receive their primary assignments.   Not every team member is as efficient as another with particular kinds of tasks.    Time required (hours) to complete tasks Task Task complexity Team member 1 - Jones Team member 2 - Nguyen Team member 3 - Walpita Team member 4 - Manderas Task A Very high 3 5 4 3 Task A High 2 1 3 2 Task...
You are part of the water supply engineering team of your council. Your team is working...
You are part of the water supply engineering team of your council. Your team is working on a certain part of cast iron piping of a water distribution system involving a parallel section. Both parallel pipes have a diameter of 30 cm, and the flow is fully turbulent. One of the branches (pipe A) is 1000 m long while the other branch (pipe B) is 3000 m long. If the flow rate through pipe A is 0.4 m3/s, determine the...
You are one of the members of the audit team of Computer express which is growing...
You are one of the members of the audit team of Computer express which is growing at a rapid rate despite being in the competitive industry. The following is part of the information obtained from a planning meeting which the audit manager had with Computer express management.    2018                       2019 K’000                     K’000 Revenue                        438,616                 373,700 Gross margin                  0.83                        0.79 Expenses as a percentage of revenue:                    Distribution costs                             0.05                        0.10 Administrative expenses 0.17                        0.13 Selling expenses...
Your role is to imagine that you are part of a team within a company that...
Your role is to imagine that you are part of a team within a company that is presenting to a group of colleagues moving to one of your international offices. You will be giving an overview of what the employees should know in order to live, work and engage in a culturally appropriate way in the international host country(India). what your colleagues would need to know to work and live in another culture. such as General information about the host...
Describe concerns to consider when developing and strengthening a team culture? For example: Team members working...
Describe concerns to consider when developing and strengthening a team culture? For example: Team members working independently or at cross-purposes, also known as "turf battles," that can slow or impede the accomplishment of project purposes. The tone of meetings and interactions that can be seen as negative, manipulative, directive, or secretive?
Imagine you and ten of your family members are in the kitchen cooking a massive feast...
Imagine you and ten of your family members are in the kitchen cooking a massive feast for thirty people. It is pure chaos in the kitchen because nobody is taking charge to ensure the needed dishes get done in a timely manner. Now switch gears and imagine you are on a multidisciplinary team at your local clinic focusing on increasing local youth access to fresh fruits and vegetables. This group does not have a leader. In your initial post, discuss...
You are one of the developers on a team of 10 working in-house for a large...
You are one of the developers on a team of 10 working in-house for a large corporation. You've been working on a new "Press Releases" page on your company's website for a few weeks. The project involves the company's Public Relations department, and the company's Social Media team. The purpose of the page is to share the company's most recent press release, and includes a list of the company's officers, and their photos. One of those company officers, the Chief...
You, and the other members of your group, are the senior marketing team for GBC Bank....
You, and the other members of your group, are the senior marketing team for GBC Bank. Your team has recently discovered and investigated the “PRIZM” consumer demographic database that Environics Analytics created for its website. Now, your team has just come out of a meeting with the President of GBC Bank. In this meeting, she announced a decision to open GBC Bank’s first retail branch. Knowing about your research, she has asked you to select a postal code neighbourhood (pick...
You and your management team are working to develop the strategic direction of your company for...
You and your management team are working to develop the strategic direction of your company for the next three years. One issue you are discussing is how to finance the projected increases in operating assets. Your options are to rely more heavily on operating creditors, borrow the funds, or to sell additional stock in your company. Discuss the pros and cons of each source of financing.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT