/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 38 Write two versions of each strin... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Write two versions of each string copy and string-concatenation function in Fig. 8.30. The first version should use array subscripting, and the second should use pointers and pointer arithmetic.

Short Answer

Expert verified
String copy and concatenation can be done using both array subscripting and pointers.

Step by step solution

01

Understanding the Problem

We need to write string manipulation functions using two methods: array subscripting and pointer arithmetic. The two main functions are string copy and string concatenation.
02

Implementing String Copy with Array Subscripting

The first version of the string copy function uses array subscripting. Define a function `str_copy_subscript(char dest[], const char src[])` where a loop iterates over the `src` array, copying each element into `dest` using a subscript.
03

Implementing String Copy with Pointers

The second version uses pointers. Define `str_copy_pointer(char *dest, const char *src)`. Using a loop, increment both `src` and `dest` pointers while copying the value from `src` to `dest` until the null character is encountered.
04

Implementing String Concatenation with Array Subscripting

The first version of string concatenation uses array subscripting. Define `str_concat_subscript(char dest[], const char src[])`, where you first find the end of `dest` using a loop, then append each element from `src`.
05

Implementing String Concatenation with Pointers

The second version uses pointer arithmetic. Define `str_concat_pointer(char *dest, const char *src)`. Move the `dest` pointer to the end of the current string, then use a loop to copy each element from `src` to `dest`, incrementing the pointers accordingly.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

String Manipulation
String manipulation in C++ involves working with sequences of characters to perform tasks like copying, concatenating, or modifying strings. Strings in C++ can be treated as arrays of characters, where each character can be accessed independently. This makes string manipulation a fundamental aspect of programming in C++. Common string manipulation tasks include:
  • Copying: Duplicating the contents of one string into another.
  • Concatenating: Joining two strings together to form a new string.
Understanding how to manage and manipulate strings effectively is crucial, as it forms the basis for handling text data in applications. For instance, copying a string might seem trivial, but in C++, it's important to know the differences between using array subscripting versus pointers for these operations.
Array Subscripting
Array subscripting is a method of accessing elements within a collection by specifying an index. In C++, arrays are zero-based, meaning the first element is accessed using index 0.
To perform operations like string copy or concatenation using array subscripting, consider the following:
  • Access elements directly by using their indexed position.
  • Use loops to traverse the array, enabling you to copy elements one-by-one.
When copying a string using array subscripting, you'd typically iterate through each character in the source array and assign it to the corresponding index in the destination array. This method is straightforward but requires careful index management to avoid going out of bounds.
Pointer Arithmetic
Pointer arithmetic involves manipulating the memory address that a pointer holds to traverse through the data it points to. In C++, pointers can be incremented, decremented, or manipulated to directly interact with the memory locations of elements.
When applied to strings, pointer arithmetic allows for:
  • Efficient Traversal: You can move through a string by incrementing the pointer.
  • Direct Memory Access: Allows manipulation at the byte level, which can be more efficient than subscripting.
For string copy and concatenation, using a pointer involves setting the initial pointer to the start of the string and moving it as each character is accessed or modified. It can be more efficient as it skips the overhead of index calculation. However, programmers should handle pointers with care to prevent memory errors.
Function Implementation
Function implementation in C++ refers to writing the code that defines the behavior of functions. Functions encapsulate specific tasks or operations, providing a way to organize and reuse code efficiently. When implementing string manipulation functions such as copy or concatenate, developers need to consider:
  • Function Signature: Define the input parameters and the return type.
  • Error Handling: Ensure the function handles edge cases, such as null pointers or empty strings.
  • Performance: Consider the efficiency of the implementation, choosing the best method (array subscripting or pointer arithmetic) based on the needs of the task.
In implementing these functions using C++, being mindful of how strings are passed and altered is important. Whether to use array subscripting or pointer arithmetic depends on the specific use case and the programmer's comfort level with each approach.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(Text Analysis) The availability of computers with string-manipulation capabilities has resulted in some rather interesting approaches to analyzing the writings of great authors. Much attention has been focused on whether William Shakespeare ever lived. Some scholars believe there is substantial evidence indicating that Christopher Marlowe or other authors actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writings of these two authors. This exercise examines three methods for analyzing texts with a computer. Note that thousands of texts, including Shakespeare, are available online at www.gutenberg.org. a. Write a program that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase To be, or not to be: that is the question: contains one "a," two "b's," no "c's," etc. b. Write a program that reads several lines of text and prints a table indicating the number of one-letter words, two-letter words, threeletter words, etc., appearing in the text. For example, the phrase Whether 'tis nobler in the mind to suffer contains the following word lengths and occurrences:c. Write a program that reads several lines of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your program should include the words in the table in the same order in which they appear in the text. For example, the lines To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer contain the words "to" three times, the word "be" two times, the word "or" once, etc. A more interesting (and useful) printout should then be attempted in which the words are sorted alphabetically.

(Printing Dates in Various Formats) Dates are commonly printed in several different formats in business correspondence. Two of the more common formats are 07/21/1955 July 21, 1955 Write a program that reads a date in the first format and prints that date in the second format

The arrays should be filled as follows: The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on".

Perform the task specified by each of the following statements: a. Write the function header for function zero that takes a long integer array parameter bigIntegers and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for function add1AndSum that takes an integer array parameter oneTooSmall and returns an integer. d. Write the function prototype for the function described in part (c)

(Writing the Word Equivalent of a Check Amount) Continuing the discussion of the previous example, we reiterate the importance of designing checkwriting systems to prevent alteration of check amounts. One common security method requires that the check amount be both written in numbers and "spelled out" in words. Even if someone is able to alter the numerical amount of the check, it is extremely difficult to change the amount in words. Write a program that inputs a numeric check amount and writes the word equivalent of the amount. Your program should be able to handle check amounts as large as $99.99. For example, the amount 112.43 should be written as ONE HUNDRED TWELVE and 43/100

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.