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 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 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;
}
Output:
Name: Bob Age: 45 Title: Programmer
Want to see more full solutions like this?
Chapter 8 Solutions
Problem Solving with C++, Student Value Edition Plus MyLab Programming with Pearson eText - Access Card Package (10th Edition)
Additional Engineering Textbook Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Starting Out with Python (3rd Edition)
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Modern Database Management
Starting Out with Programming Logic and Design (4th Edition)
Database Concepts (7th Edition)
- Write a whole program using c++ to ask the user for his/her name, age, and gpa, then print on the screen "Hello NAME!, you are AGE years old and your Grade Point Average is GPA.", where NAME, AGE, and GPA are replaced by the values that the user typed. User data should be stored in a struct variable of type userType, which has three components: name of type string, age of type int, and gpa of type double.arrow_forwardWrite a C++ function named getLowerAlphabets that takes a C-string argument and returns a C-stringmade up of the lower alphabet characters of the argument. If none of the characters of the argument is alower alphabet then you must still return a C-string which is obviously empty C-string; that is a C-stringwith only the null character.arrow_forwardWrite a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayedarrow_forward
- Write a c++ program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Your program must contain at least the following functions: A function to read the students’ data into the array. A function to assign the relevant grade to each student. A function to find the highest test score. A function to print the names of the students having the highest test score. Your program must output each student’s name in this form: last name followed by a comma, followed by…arrow_forwardWrite a function that replaces a substring with a new string. Example: >>> replace('The quick brown fox jumps over the lazy dog', 'quick', 'slow') The slow brown fox jumps over the lazy dog The first parameter is the string, second is the substring to be replaced and the third is the string to replace the substring. If there are more than one occurrence of a substring, replace it all.arrow_forwardWrite a program that inputs a student's name in the following form: lastName, firstName middleName. The program will convert the name to the following form: firstName middleName lastName. Your program must read the student's entire name in one variable and must consist of a user-defined function that takes as input a string, consisting of a student's name, and returns the string consisting of the altered name. You can use the string function find to find the index of ,(the comma); the function length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName.arrow_forward
- Write a C++ Program that calculates the area and perimeter of an upright rectangle.Create a struct Point that has two double values, x and y. Write a function that asks the user for a Point.Call that function twice, once to get the bottom left corner of a rectangle, and once to get the top right corner.Write another function that takes two Points and returns area and perimeter. Print out the area and perimeter.Given two points (x1, y1) and (x2, y2), the formulas to compute perimeter and area are as followsPerimeter = 2 ∗ (abs(x1 − x2) + abs(y1 − y2))Area = abs(x1 − x2) ∗ abs(y1 − y2)arrow_forwardWrite a complete C++ program that reads from a comma-separated string of name values (entered by standard input) and prints each name on its own line. (This format for data is called CSV, which stands for comma-separated values.) The end of the CSV input will be signaled by two commas in a row. For example, if the CSV-formatted input string is "Aaron Rodgers,J. J. Watt,Luke Kuechly,," then the output should be: Aaron Rodgers J. J. Watt Luke Kuechly HINT: The getline function takes an optional third parameter: a delimiting character. By default, the delimiter is the newline, but you can change this by adding another parameter.arrow_forwardI have a string with a phrase in it, and I want to turn it into all caps using the function toupper. Write a code fragment that does that and then prints the resulting string.arrow_forward
- Write the definition of the string function named L() that takes no parameters and returns a string of a line of 10 asterisks.arrow_forwardWrite a c program to ask the user for a string code, if the user enters the correct code which is “peace”, then allow the user to enter a string that must be less than 35 characters long. If the user enters a wrong code, keep asking until the user enters the correct code. After the user enters the string, find the length of the string, then pass the string (array) to a function that will count the number of character ‘a’ or ‘A’ and returns the count. The calling function gets the number of counts and displays the string plus the count.arrow_forwardSuppose a String name is given in a format where the firstname and lastname are separated by a semicolon like ‘Ziaul;Hossain’ ; the general format would be ‘firstname;lastname’ (firstname and lastname can be any String). Write a program which will separate the lastname into a different string. (Hint: use a function from String class to find the position of semicolon and then take substring from that position).arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning