Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 8, Problem 1P

Create a C-string variable that contains a name, age, and title. Each field is separated by a space. For example, the string might contain “Bob 45 Programmer” or any other name/age/title in the same format. Assume the name, age, and title have no spaces themselves. Write a program using only functions from cstring (not the class string) that can extract the name, age, and title into separate variables. Test your program with a variety of names, ages, and titles.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

  • Include the necessary header files.
  • Declare the namespace.
  • Define the “main()” function.
    • Declare the necessary variables.
    • Initialize the character array.
    •  Use the functions “strtok()” to split the string into tokens.
    • Use the function “strcpy()” to copy the source string into the destination string.
    • Print the result.
Program Description Answer

Program to display name, age and title into separate variables using the function “cstring”.

Explanation of Solution

Program:

//Include the header file iostream

#include<iostream>

//Include the header file cstring

#include<cstring>

//Declare the namespace

using namespace std;

//Define the main() function

int main()

{

  //Declare the necessary variables

  char name[20];

  char age[4];

  char title[50];

  //Initialize the char array

  char res[] = "Bob 45 Programmer";

 /*Call the function strtok() to split str into tokens and assign the result in *pch*/

  char *pch = strtok(res," ");

/*Call the function strcpy to copy the value in pch into     name*/

  strcpy(name,pch);

/*Call the function strtok() to split str into tokens and assign the result in *pch*/

  pch = strtok(NULL," ");

/*Call the function strcpy to copy the value in pch into age*/

  strcpy(age,pch);

/*Call the function strtok() to split str into tokens and assign the result in *pch*/

  pch = strtok(NULL," ");

/*Call the function strcpy to copy the value in pch into title*/

  strcpy(title,pch);

  //print the name

  cout<<"Name: "<<name<<" ";

  //print the age

  cout<<"Age: "<<age<<" ";

  //Print the title

  cout<<"Title: "<<title;

  //Return zero

  return 0;

}

Sample Output

Output:

Name: Bob Age: 45 Title: Programmer

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
In C programming language, the name of a variable is a string that can contain uppercase letters, lowercase letters, digits, or underscores. Further, the first character in the string must be a letter, either uppercase or lowercase, or an underscore. If the name of a variable is determined by its first eight characters, how many different variables can be named in C? (Note that the name of a variable may contain fewer than eight characters.)
Create a function that takes a string of words and returns the word with the highest score. Each letter of the word gets points according to its position in the alphabet: a = 1, b = 2, C = 3, etc. Examples wordRank ("A quick brown fox.") → "brown" wordRank ("Nancy is very pretty.") "pretty" wordRank ("Come back tomorrow, man!") → "tomorrow" wordRank ("Wednesday is hump day.") "Wednesday" Solution in JS 10
C programming. Write a program that gets a string from the user and maps the capital letters into small letters, and maps the small letters into capital letters. Namely, 'A' should be changed to 'a'; 'B' should be changed to 'b'; ... 'Z' should be changed to 'z'. Similarly, 'a' should be changed to 'A'; 'b' should be changed to 'B'; etc... Your program should also change the digits into next odd number. Namely, '0' should be changed to '1'; '1' should be leaved  as '1'; '2' should be changed to '3'; ... '9' should be leaved as '9'. Your program should print the final form of the altered string.    Example: If the user enters:  My salary is 1000 TL. The output should be:  mY SALARY IS 1111 tl

Chapter 8 Solutions

Problem Solving with C++ (10th Edition)

Ch. 8.1 - What string will be output when this code is run?...Ch. 8.1 - Prob. 12STECh. 8.1 - Consider the following code (and assume it is...Ch. 8.1 - Consider the following code (and assume it is...Ch. 8.2 - Consider the following code (and assume that it is...Ch. 8.2 - Prob. 16STECh. 8.2 - Consider the following code: string s1, s2...Ch. 8.2 - What is the output produced by the following code?...Ch. 8.3 - Is the following program legal? If so, what is the...Ch. 8.3 - What is the difference between the size and the...Ch. 8 - Create a C-string variable that contains a name,...Ch. 8 - Prob. 2PCh. 8 - Write a program that inputs a first and last name,...Ch. 8 - Write a function named firstLast2 that takes as...Ch. 8 - Write a function named swapFrontBack that takes as...Ch. 8 - Prob. 6PCh. 8 - Write a program that inputs two string variables,...Ch. 8 - Solution to Programming Project 8.1 Write a...Ch. 8 - Write a program that will read in a line of text...Ch. 8 - Give the function definition for the function with...Ch. 8 - Write a program that reads a persons name in the...Ch. 8 - Write a program that reads in a line of text and...Ch. 8 - Write a program that reads in a line of text and...Ch. 8 - Write a program that can be used to train the user...Ch. 8 - Write a sorting function that is similar to...Ch. 8 - Redo Programming Project 6 from Chapter 7, but...Ch. 8 - Redo Programming Project 5 from Chapter 7, but...Ch. 8 - Prob. 11PPCh. 8 - Write a program that inputs a time from the...Ch. 8 - Solution to Programming Project 8.14 Given the...Ch. 8 - Write a function that determines if two strings...Ch. 8 - Write a program that inputs two strings (either...Ch. 8 - Write a program that manages a list of up to 10...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Computer Programming for Beginners | Functions, Parameters & Arguments | Ep24; Author: Programming With Avelx;https://www.youtube.com/watch?v=VXlh-qJpfw0;License: Standard YouTube License, CC-BY