Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / / / getWord ( ) / / / / Purpose: / / Prompt the user to enter a string / / Return string

//
// getWord()
//
// Purpose:
// Prompt the user to enter a string
// Return string via output pointer parameter
//
// Parameters:
// string *word -- pointer to a string that will contain the string the user entered
//
// Return:
// void
//
void getWord(string *word)
{
cout << "Enter a word: " ;
/* Add your code here that performs the cin and sets the word parameter */
}
//
// convertStringCharArray()
//
// Purpose:
// convert the given string into an dynamically allocated array of characters
//
// Parameters:
// string word -- the string that has all the letters that will be used to populate the dynamic char array
// char **charArray --(output) pointer to a pointer that refer to the newly allocated memory and upon return
// will contain all the letters from the string word parameter + null terminating character
// Returns:
// Length of the string (not including null character)
//
// Notes:
// Caller is responsible for deleting memory allocated by this function
//
int convertStringCharArray (string word, char **charArray)
{
size_t len = word.length();
//
// Dynamically allocate memory to store all the letters from word
// into a "new" array of chars
// Be sure to allocate 1 extra byte for the '\0' null string character terminator
//
/* Add your code here that uses the new operator to create a dynamic array */
/* Add your code here that uses a for loop to copy all the letters from the word string variable */
/* to the dynamically allocated array. Note to get full points you must use a single statement */
/* with the pointer increment operator (++) and the deference '*' operator */
/* Add your code here to add a '\0' string null terminator to the end of the dynamically allocated array*/
/* Add your code here to set the charArray output parameter */
return len;
}
//
// deallocateMemory()
//
// Purpose:
// returns the memory used by the dynamic array produced by the convertStringCharArray function
//
// Parameters:
// char *arr -- pointer to the dynamically allocated array
//
// Returns:
// void
//
// remember you must use delete[](not just delete) when deleting dynamic arrays
//
void deallocateMemory(char *arr)
{
/* Add your code here the returns the memory referenced by the dyanamically allocated array */
/* passed in as the (arr) parameter */
// remember the delete[] operator (with the []) must be used to release memory allocated to a dynamic array
}
//
// printCharArray()
//
// Purpose:
// Demonstrate the use of pointer arithmetic to traverse the dynamically allocated
// by printing all the characters in the array forward and backwards
//
// Parameters:
// char *arr -- pointer to the dynamically allocated array
//
// Returns:
// void
//
void printCharArray(char* arr)
{
// note to receive credit for this part, your code cannot use the index [] operator
// print the contents of the array in the forward direction
char* p = arr;
while (*p)
{
cout <<*p++;
}
p--; // move back to the last character (p was previously pointing to the null terminator)
cout << endl;
while (p != arr)
{
/* add your code here the prints out the characters in reverse order pointed by p */
/* after printing the character pointed to by p, decrement p to move the previous character*/
}
cout <<*p << endl; // print the first character
}

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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

The following equilibrium were attained at 823 K:

Answered: 1 week ago

Question

5. Do you have any foreign language proficiency?

Answered: 1 week ago