Chapter 8: Problem 35
Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the phone number as a token, and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed.
Short Answer
Step by step solution
Understand the Problem
Prepare the Input
Initialize strtok Function
Extract Area Code
Extract First Three Digits
Extract Last Four Digits
Concatenate Phone Number Digits
Print Output
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 Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
strtok function
1. A pointer to the string that is being tokenized.
2. A string of delimiters, which can be one or more characters indicating the boundaries of each token. Examples include space " ", comma ",", dash "-", and others.
Initially, `strtok` requires the memory address of the string to begin tokenization. On subsequent calls, it uses `NULL` as the first parameter to continue from where it left off. It returns the next token or `NULL` if there are no more tokens. Here's a simple way to understand its usage:
- Start with a string: "(555) 555-5555".
- Use `strtok` with delimiters like "() -" to break down the parts of the phone number.
- First token: Area code, then followed by the other parts.
string concatenation
In the context of our exercise, the last seven digits of a phone number must be combined into one string. Here's how string concatenation can be accomplished in C++:
- Using the `+` operator: If you are dealing with `std::string` objects, you can concatenate using the `+` operator. For example: `std::string fullNumber = part1 + part2;`
- Using `strcat`: In C-strings, `strcat` function is used but requires caution. Ensure the destination string is sufficiently large to hold the entire content.
token extraction
In our example, token extraction is achieved through the `strtok` function, which provides a straightforward way to parse a sequence by specifying delimiters. The process involves:
- Identifying the delimiters, such as parentheses or dashes, that separate desired tokens.
- Applying `strtok` to isolate segments such as area code ("555"), prefix ("555"), and line number ("5555").
- Retrieving each section individually for further processing or display.
programming exercises
During these exercises, key goals are to:
- Apply theoretical knowledge to practical problems, like extracting and reformatting data.
- Increase familiarity with C++ standard libraries, particularly those related to string handling.
- Understand how to debug and refine solutions iteratively for better performance and accuracy.