1. #include <iostream>
#include <cstring>
using namespace std;
//PROJECT 1 STUDENT FILE
//Postcondition: The value in parameter a contains the value that
was in parameter b and
//the value of parameter b contains the value that was in
parameter a
template <class type> void swapIt(type &a, type &b);
//Postcondition: Returns true if all characters in the string
(dynamic character array)
//are a numerical digit (0,1,2,3,4,5,6,7,8,9). Otherwise the
function returns false
bool isDigitString(const char *);
//Postcondition: Reverses the order of the characters in the
//string (dynamic character array)
void reverse(char *);
//Postcondition: Prints the addresses of each memory location of
the dynamic array
template <class type> void printAddresses(type *, int);
//Postcondition: Prints the ASCII decimal code for each
character in the
//string (dynamic character array)
void printASCII(char *);
//test drivers for functions
void test_isDigitString();
void test_reverse(char *);
int main()
{
//you may want to comment out certain code below so you can
do
//more specific testing or add your own testing
//testing swapIt function with integers
int x = 2, y = 8;
cout << "Before swap x = " << x << " and y = " << y <<
2. endl;
swapIt(x,y);
cout << "After swap x = " << x << " and y = " << y <<
endl;
//testing swapIt function with booleans
bool a = true, b = false;
cout << boolalpha;
cout << "Before swap x = " << a << " and y = " << b <<
endl;
swapIt(a,b);
cout << "After swap x = " << a << " and y = " << b <<
endl;
//test if isDigitString function is working properly
test_isDigitString();
cout << endl;
//test if reverse function is working properly
char str[] = "This is a test";
test_reverse(str);
cout << endl;
//test if printAddresses function is working properly
printAddresses(str, strlen(str));
cout << endl;
//test if printASCII function is working properly
printASCII(str);
cout << endl;
}
//Postcondition: The value in parameter a contains the value that
was in parameter b and
//the value of parameter b contains the value that was in
parameter a
template <class type>
void swapIt(type &a, type&b)
3. {
//FILL IN NECESSARY CODE
}
//Postcondition: Returns true if all characters in the string
(dynamic character array)
//are a numerical digit (0,1,2,3,4,5,6,7,8,9). Otherwise the
function returns false
bool isDigitString(const char *theString)
{
//FILL IN NECESSARY CODE
}
//Postcondition: Reverses the order of the characters in the
//string (dynamic character array)
void reverse(char *theString)
{
//FILL IN NECESSARY CODE
}
//Postcondition: Prints the addresses of each memory location of
the dynamic array
template <class type>
void printAddresses(type *item, int n)
{
//FILL IN NECESSARY CODE
}
//Postcondition: Prints the ASCII decimal code for each
character in the
//string (dynamic character array)
void printASCII(char *theString)
{
//FILL IN NECESSARY CODE
4. }
void test_isDigitString()
{
bool tryAgain = false;
int size;
cout << "Enter the maximum size for your string. ";
cin >> size;
char *str = new char[size];
do
{
cout << "Enter a positive integer. ";
cin >> str;
if(isDigitString(str))
{
cout << "You have entered the positive integer " <<
str << endl;
tryAgain = false;
}
else
{
cout << "You have entered incorrect data. Please try
again. ";
tryAgain = true;
}
}while(tryAgain);
delete str;
}
void test_reverse(char *theString)
{
reverse(theString);
cout << "Your string in reverse is: " << theString << endl;
5. }
Solution
#include <iostream>
#include <cstring>
using namespace std;
//PROJECT 1 STUDENT FILE
//Postcondition: The value in parameter a contains the value
that was in parameter b and
//the value of parameter b contains the value that was in
parameter a
template <class type> void swapIt(type &a, type &b);
//Postcondition: Returns true if all characters in the string
(dynamic character array)
//are a numerical digit (0,1,2,3,4,5,6,7,8,9). Otherwise the
function returns false
bool isDigitString(const char *);
//Postcondition: Reverses the order of the characters in the
//string (dynamic character array)
void reverse(char *);
//Postcondition: Prints the addresses of each memory location
6. of the dynamic array
template <class type> void printAddresses(type *, int);
//Postcondition: Prints the ASCII decimal code for each
character in the
//string (dynamic character array)
void printASCII(char *);
//test drivers for functions
void test_isDigitString();
void test_reverse(char *);
int main()
{
//you may want to comment out certain code below so you can
do
//more specific testing or add your own testing
//testing swapIt function with integers
int x = 2, y = 8;
cout << "Before swap x = " << x << " and y = " << y <<
endl;
swapIt(x,y);
cout << "After swap x = " << x << " and y = " << y << endl;
//testing swapIt function with booleans
bool a = true, b = false;
cout << boolalpha;
cout << "Before swap x = " << a << " and y = " << b <<
7. endl;
swapIt(a,b);
cout << "After swap x = " << a << " and y = " << b << endl;
//test if isDigitString function is working properly
test_isDigitString();
cout << endl;
//test if reverse function is working properly
char str[] = "This is a test";
test_reverse(str);
cout << endl;
//test if printAddresses function is working properly
printAddresses(str, strlen(str));
cout << endl;
//test if printASCII function is working properly
printASCII(str);
cout << endl;
}
//Postcondition: The value in parameter a contains the value
that was in parameter b and
//the value of parameter b contains the value that was in
parameter a
template <class type>
8. void swapIt(type &a, type&b)
{
//FILL IN NECESSARY CODE
type temp;
temp = a;
a = b;
b = temp;
}
//Postcondition: Returns true if all characters in the string
(dynamic character array)
//are a numerical digit (0,1,2,3,4,5,6,7,8,9). Otherwise the
function returns false
bool isDigitString(const char *theString)
{
//FILL IN NECESSARY CODE
int i = 0;
while(*theString != '0')
if(!isdigit(*theString))
return false;
return true;
}
//Postcondition: Reverses the order of the characters in the
//string (dynamic character array)
void reverse(char *theString)
9. {
//FILL IN NECESSARY CODE
int len = strlen(theString);
for(int i = len -1 ; i >= 0; i--)
cout<<*theString<<endl;
}
//Postcondition: Prints the addresses of each memory location
of the dynamic array
template <class type>
void printAddresses(type *item, int n)
{
//FILL IN NECESSARY CODE
for(int i = 0; i < n; i++)
cout<<(item + i)<<" ";
cout<<endl;
}
//Postcondition: Prints the ASCII decimal code for each
character in the
//string (dynamic character array)
void printASCII(char *theString)
10. {
//FILL IN NECESSARY CODE
int i = 0;
while(*(theString + i) != '0')
cout<<*(theString + i)<<" ";
cout<<endl;
}
void test_isDigitString()
{
bool tryAgain = false;
int size;
cout << "Enter the maximum size for your string. ";
cin >> size;
char *str = new char[size];
do
{
cout << "Enter a positive integer. ";
cin >> str;
if(isDigitString(str))
{
cout << "You have entered the positive integer " << str <<
endl;