C programming Imagine you are working on a team and one of y

C programming:

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.

Each unit test must use assert exactly once (in addition to other code). You will need to compile mystringfunctions.c into an object file called mystringfunctions.o and link it with your executable, which you will call testmystringfunctions.

(mystringfunctions.h)

#ifndef MYSTRINGFUNCTIONS
#define MYSTRINGFUNCTIONS

#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

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

(mystringfunctions.c)

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#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;
}

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

(testmystringfunctions.c)

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.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.
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() {
char c;
while ( (c = getchar()) != EOF &&
c != \'\ \' &&
c != \'\ \' ) {};
}

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

example output:

Solution

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

i= 0 and n is that character

char ch1[10],ch2[[10],ch3[20]

while(i!=n)

{

ch1[i]=get(str[i])

i++;

}

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

j=0

while(j!=n1)

{

ch2[j]=get(str[j])

j++;

}

for concate two string

i=0;,j=0,k=0;

while(k!=n1)

{

if(i<=n)

{

ch3[k]=ch1[i]

i++,k++;

}

else

{

ch3[k]=ch2[j]

j++,k++;

}

}

C programming: 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 thei
C programming: 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 thei
C programming: 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 thei
C programming: 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 thei
C programming: 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 thei

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site