Question
This assignment is an exercise in writing, compiling, and executing a C++ program on the departmental UNIX system. It also covers the manipulation of C
This assignment is an exercise in writing, compiling, and executing a C++ program on the departmental UNIX system. It also covers the manipulation of C strings
The code for the driver program is shown below:
/**
* CSCI 241 Assignment 1
*
* Author: your name
dog tag number:
* This program tests functions for manipulating C strings.
*/
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
// Add your function prototypes here
int main()
{
const int WORDSIZE = 15;
char words[][WORDSIZE] = {"sprightful", "reason to row", "New York", "Bolton", "Frida", ""};
char word[WORDSIZE];
// Test the copy function
cout << "Copy \"sprightful\", should see \"sprightful\". ";
copy(word, words[0], sizeof(word) - 1);
cout << word << endl << endl;
// Test the limit on the copy function
cout << "Copy \"Supercalifragilisticexpialidocious\", should see \"Super\". ";
copy(word, "Supercalifragilisticexpialidocious", 5);
cout << word << endl << endl;
// Test the replaceCopy function
cout << "Replace 'r' in \"reason to row\" with 's', should see \"season to sow\". ";
replaceCopy(word, words[1], 'r', 's', sizeof(word) - 1);
cout << word << endl << endl;
// Test the limit on the replaceCopy function
cout << "Replace 's' in \"Supercalifragilistic\" with 'd', should see \"duper\". ";
replaceCopy(word, "Supercalifragilistic", 'S', 'd', 5);
cout << word << endl << endl;
// Test the caseChangeCopy function
cout << "Case change \"New York\", should see \"nEW yORK\". ";
caseChangeCopy(word, words[2], sizeof(word) - 1);
cout << word << endl << endl;
// Test the limit on the caseChangeCopy function
cout << "Case change \"Supercalifragilistic\", should see \"sUPER\". ";
caseChangeCopy(word, "Supercalifragilistic", 5);
cout << word << endl << endl;
// Test the reverseCopy function
cout << "Reverse \"Bolton\", should see \"notloB\" ";
reverseCopy(word, words[3], sizeof(word) - 1);;
cout << word << endl << endl;
// Test the limit on the reverseCopy function
cout << "Reverse \"cytogeneticists\", should see \"tsicitenegotyc\" ";
reverseCopy(word, "cytogeneticists", sizeof(word) - 1);;
cout << word << endl << endl;
cout << "Reverse \"Frida\", change case, and replace 'D' with 'Z', should see \"AZIRf\". ";
replaceCopy(caseChangeCopy(reverseCopy(word, words[4], sizeof(word) - 1), word, sizeof(word) - 1), word, 'D', 'Z', sizeof(word) - 1);
cout << word << endl << endl;
cout << "Enter your entire name: ";
read(words[5], sizeof(words[5]));
cout << words[5] << endl << endl;
cout << "Reverse your name and change case. ";
cout << caseChangeCopy(reverseCopy(word, words[5], sizeof(word) - 1), word, sizeof(word) - 1) << endl;
return 0;
}
// Add your function definitions here
Write the following functions and add them to assign1.cpp:
- char* copy(char* destination, const char* source, size_t num)
Copy the first num characters of a string from the address specified by source to that specified by destination. Note that source and destination are both pointers. Return a pointer to the start of the copied string.
- char* reverseCopy(char* destination, const char* source, size_t num)
Copy the first num characters of a string from the address specified by source to that specified by destination, reversing the order of the characters. Return a pointer to the start of the copied string.
- char* caseChangeCopy(char* destination, const char* source, size_t num)
Copy the first num characters of a string in which the case of each lowercase character is shifted to uppercase (and uppercase to lowercase), from the address specified by source to that specified by destination. Return a pointer to the start of the copied string.
You can use the character functions found in the C standard library header file to determine whether a character is an uppercase or lowercase letter, and to convert letters from one case to the other.
- char* replaceCopy(char* destination, const char* source, char target, char replace, size_t num)
Copy the first num characters of a string in which each instance of a "target" character is replaced by the "replace" character, from the address specified by source to that specified by destination. Return a pointer to the start of the copied string.
- void read(char* destination, int num)
Read a string of characters entered by the user at the keyboard with a length no longer than num characters (including the null character at the end of the C string) into an address specified by destination. The user should be able to type a space or tab as part of the string.
The getline() method of the istream class in the header file can be used to read a string that contains spaces or tabs into an array of char.
Feel free to use the existing C string manipulation functions in the C standard library header file when coding the functions required for this assignment.
Sample Output
A sample run of the program is shown below.
Copy "sprightful", should see "sprightful".
sprightful
Copy "Supercalifragilisticexpialidocious", should see "Super".
Super
Replace 'r' in "reason to row" with 's', should see "season to sow".
season to sow
Replace 's' in "Supercalifragilistic" with 'd', should see "duper".
duper
Case change "New York", should see "nEW yORK".
nEW yORK
Case change "Supercalifragilistic", should see "sUPER".
sUPER
Reverse "Bolton", should see "notloB"
notloB
Reverse "cytogeneticists", should see "tsicitenegotyc"
tsicitenegotyc
Reverse "Frida", change case, and replace 'D' with 'Z', should see "AZIRf".
AZIRf
Enter your entire name:
Reverse your name and change case.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started