Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the following suggested test cases and hints! C++!!! 7.19 Lab: String Functions In this lab you will write a number of useful functions that

Use the following suggested test cases and hints! C++!!!

7.19 Lab: String Functions

In this lab you will write a number of useful functions that transform strings.

To help you in this task, you will also write some unit tests to verify that your functions are working correctly.

The starter code contains a skeletal test harness, which you will extend as needed to run the tests on your functions.

The String Functions

The functions that you will write (and which will be graded) are:

string strToUpper(string)

  • Converts all alphabetic characters to uppercase.
    • For example, "STIR Crazy!" becomes "STIR CRAZY!"

string strToLower(string)

  • Converts all alphabetic characters to lowercase.
    • For example, "STIR Crazy!" becomes "stir crazy!"

string strToTitle(string)

  • Converts the string to "Title" case.
    • For example, "the grapes of wrath" becomes "The Grapes Of Wrath"
    • .. that is, the first letter of each word is made uppercase
    • .. and the remaining letters are made lowercase

string strReverse(string)

  • Returns the string with the characters in reverse order.
    • For example, "Hello" becomes "olleH"

string strAlphaOnly(string)

  • Returns the string with all non-alphabetic characters removed.
    • For example, "R2D2 and C3P0" becomes "RDandCP"

The Test Harness

The main() function invokes five "helper" functions to exercise your string functions. Each of these functions returns an integer value that represents the number of failing test cases in that batch. By accumulating the number of failures from each call, we can display the total number of failing tests:

// Run unit tests; report failure count. int main() { int failures = 0; cout << "Running tests..." << endl; // invoke test functions failures += runStrToUpperTests(); failures += runStrToLowerTests(); failures += runStrToTitleTests(); failures += runStrReverseTests(); failures += runStrAlphaOnlyTests(); // summary report cout << " Number of test failures: " << failures << endl << endl; return 0; } 

The helper function runStrToUpperTests() is written for you. It makes use of another helper function to run several tests of the strToUpper() function, counting and returning the number of failures.

(The reason for writing so many layers of functions is to reduce the amount of duplicated code.)

int runStrToUpperTests() { int fails = 0; cout << endl; fails += testStrToUpper("fooBar.", "FOOBAR."); fails += testStrToUpper("Hey, You!", "HEY, YOU!"); fails += testStrToUpper("12345", "12345"); fails += testStrToUpper("HELP me", "HELP ME"); return fails; } 

The two arguments to testStrToUpper(...) are:

  • the input string
  • the expected output string

The testStrToUpper(...) helper function is also written for you. It invokes a general purpose "string function tester" which tests a string function invocation and prints a neat summary of the test:

int testStrToUpper(string input, string expected) { return testStrFn("strToUpper()", input, expected, strToUpper(input)); } 

Here the arguments are:

  • a label identifying the function being tested
  • the input string
  • the expected output string
  • the result of calling the function with the input string

This general purpose function returns 1 for a failing test or 0 for a passing test.

The output of such a call might look like this:

 pass : strToUpper() : "fooBar." -> "FOOBAR." 

Look for the TODO comments in the starter code to see where you need to implement your solution code.

More Test Cases

Here are some suggested test cases for you to use:

strToLower()

"fooBar." --> "foobar." "Hey, You!" --> "hey, you!" "12345" --> "12345" "HELP me" --> "help me" 

strToTitle()

"the grapes of wrath" --> "The Grapes Of Wrath" "STAR WARS" --> "Star Wars" "How do you do?" --> "How Do You Do?" "one FISH, two FISH." --> "One Fish, Two Fish." 

strReverse()

"12345" --> "54321" "Happy New Year!" --> "!raeY weN yppaH" "Hannah" --> "hannaH" "gel" --> "leg" 

strAlphaOnly()

"Z0o-m54b++i@e" --> "Zombie" "Office-365" --> "Office" "Computers" --> "Computers" "R2D2 and C3P0" --> "RDandCP" 

Useful References

You might find the following functions helpful

  • int toupper(int c)
  • int tolower(int c)
  • int isspace(int c)
  • int isalpha(int c)

These functions (defined in the header file) work with char values, despite being defined with int return value and parameter.

 toupper('a') // 'A' tolower('T') // 't' isspace(' ') // true isspace('.') // false isalpha('w') // true isalpha('?') // false 

Hints

1) You can determine the length of a string str with str.length() or str.size().

2) You can access the i'th character of a string str with str.at(i) or str[i].

3) You will probably want to use a for loop to iterate over the characters of the input string.

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

Step: 3

blur-text-image

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

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago