/*! 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 37 Write a program that inputs a te... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

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

Expert verified
The program will read a telephone number string, use strtok to tokenize the area code and the phone number, concatenate the phone number into one string, and then print both the area code and phone number.

Step by step solution

01

Understand the Problem

The task is to write a program that extracts and prints an area code and a phone number from an input string formatted like (555)555-5555. The extraction will use the function strtok to tokenize the string, and then concatenate the first three digits with the last four digits to form the complete phone number.
02

Include Necessary Headers

Include necessary headers in the C program for input/output operations and string manipulations - namely stdio.h for printf and scanf, and string.h for strtok.
03

Read Input From the User

Prompt the user to enter a telephone number in the specified format, and read it in as a string.
04

Use strtok to Tokenize the String

Use the strtok function to tokenize the input string. First, extract the area code by passing the input string and parentheses as delimiters to strtok. Then extract the next part of the number (first three digits), and finally the last four digits, using a hyphen as an additional delimiter.
05

Concatenate the Phone Number

After tokenizing the string, concatenate the first three digits with the last four digits to make the complete phone number.
06

Print the Results

Print out the area code and the concatenated phone number in a clear and comprehensible format.

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.

strtok function
In C++, string tokenization is a powerful tool for dissecting a string into multiple segments, called tokens, based on specified delimiters. The strtok function is a classic function used in this process. It comes from the C library and is also available in C++ through the cstring or string.h header. Understanding how to use strtok is fundamental for many text-parsing tasks.

The function works by taking two arguments: the string you want to tokenize and a string containing all possible delimiters. On the first call, strtok takes the string to be tokenized, and on subsequent calls, it should be passed NULL to indicate continuation from where it left off. The function then returns a pointer to the next token, or NULL when no more tokens are available.

In our exercise, we used strtok to extract different components of a telephone number. Delimiters like parentheses and hyphens were used to isolate the area code and phone number segments. Each call to strtok gives us the next segment of the phone number until the entire sequence is processed. Knowing how to implement and sequence calls to strtok is a critical skill in C++ programming for data parsing tasks.
string manipulation
String manipulation involves altering, dissecting, and recombining strings to achieve a desired output. In C++, strings can be manipulated in numerous ways, including tokenization, concatenation, searching, and more. The standard string class provides a rich set of functions that make these tasks easier, but traditional C-style string manipulation functions - like those found in cstring - are still widely used for simplicity and direct control over strings.

After tokenizing the input string using strtok, we perform another common operation: concatenation. This is the process of appending one string to the end of another to create a single string. This technique was used to join the two parts of the phone number in our exercise. While C++'s string class offers convenient concatenation using the + operator, C-style strings require careful handling with functions like strcat or by manually iterating and combining the characters. String manipulation is ubiquitous in programming, and mastering it in C++ requires practice and an understanding of both the string class and C-style string functions.
C++ programming basics
Grasping the basics of C++ programming is vital for solving a wide range of problems, from simple to complex. C++ is a versatile language that supports procedural, object-oriented, and generic programming paradigms. For beginners, it's essential to learn the syntax, data types, control structures, functions, and standard library utilities, like input/output operations and string handling.

In the exercise, we included headers such as stdio.h for Input/Output operations using functions like printf and scanf, and string.h for string manipulation functions such as strtok. These headers provide access to a wide variety of functions that are a part of the standard C and C++ libraries.

The problem-solving process typically begins with understanding the problem and then follows a structured approach involving reading input, processing data, and producing output. This step-by-step methodology, demonstrated in the exercise to extract and format a telephone number, serves as a great example of how fundamental C++ programming constructs can be applied to real-world problems.

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

The left-shift operator can be used to pack two character values into a two- byte unsigned integer variable. Write a program that inputs two characters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned integer variable, assign the first character to the unsigned variable, shift the unsigned variable left by eight bit positions and combine the unsigned variable with the second character using the bitwise inclusive-OR operator. The program should output the characters in their bit format before and after they're packed into the unsigned integer to prove that they're in fact packed correctly in the unsigned variable.

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 that Francis Bacon, Christopher Marlowe or other authors actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writings of these authors. This exercise examines three methods for analyzing texts with a computer. 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, three-letter 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 word "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.

Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. Use only the C-style string-processing techniques shown in this chapter.

Write a program that will assist the user with metric conversions. Your program should allow the user to specify the names of the units as strings (i.e., centimeters, liters, grams, etc., for the metric system and inches, quarts, pounds, etc., for the English system \()\) and should respond to simple questions such as "How many inches are in 2 meters?" "How many liters are in 10 quarts?" Your program should recognize invalid conversions. For example, the question "How many feet are in 5 kilograms?" is not meaningful, because "feet" are units of length, while "kilograms" are units of weight.

Write a single statement or a set of statements to accomplish each of the following: a) Define a structure called Part containing int variable partNumber and char array partName, whose values may be as long as 25 characters. b) Define PartPtr to be a synonym for the type Part c) Use separate statements to declare variable a to be of type Part, array b[ \(10]\) to be of type Part and variable ptr to be of type pointer to Part. d) Read a part number and a part name from the keyboard into the members of variable a. e) Assign the member values of variable a to element three of array b. f) Assign the address of array b to the pointer variable ptr. g) Print the member values of element three of array b, using the variable ptr and the structure pointer operator to refer to the members.

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.