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++ (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)
- Java Program - Functions With Parameters and No Return Values Create a function named palindrome that asks the user a string input if called. If the string is a palindrome, print “{input} is a palindrome!”, otherwise print “{input} is not a palindrome!”. In the main function, call the palindrome function. Input 1. One line containing a single string Output Enter a string: racecar racecar is a palindrome!arrow_forwardWrite a program that inputs the first name, middle initial (without the period), and last name of a user and displays that person’s name with the first name first, middle initial followed by a period, and last name last. You will need the following variables:FirstName (a String) MiddleInitial (a String)LastName (a String) using c programming.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 program called palindrome.cpp. The program should have the two functions listed below. Your program should loop until the user enters “quit". The determination of a word being a palindrome or not should be case insensitive. void getWord ( string& word ): This function is responsible for getting the word from the user. void isPalindrome ( string& word, bool& result ): This function is responsible for determining if a word is a palindrome or not. It will return true if the word is a palindrome and false if the word is not a palindrome using a boolean variable passed by reference. Sample Output 1 What is the word? Kayak Kayak is a palindrome Sample Output 2 What is the word? APPLE APPLE is not a palindrome Notes • Remember that you can access each character in a string by using the index operator, i.e., word[3], and the highest index of any string is the length of the string minus one. • To handle the case-insensitivity part you may need to work with functions that change the case…arrow_forwardHelp me homework c++ The shots file holds a list of shots (imagine some hitscan weapon in a video game like a shotgun or something). Each shot has an origin and a direction. The origin is an (x,y) coordinate, like (5,3). The direction is a slope and whether the shot is traveling along that slope or in the reverse. A slope of "Vertical" means that the shot is travellingstraight up and down. The format is:x_location y_location slope(either a number like 2.1 or a non-number meaning"Vertical") forwards(1 meaning forwards, 0 meaning backwards) 0 0 0 00 0 0 10 0 Vertical 00 0 Squirrel 110 10 -1 1-10.1 -100.01 2.1 0 The first line is a horizontal line shooting left from the origin (0,0).The second line is a shot also travelling horizontally from the origin, but forward along the x axis instead of backwards.The third line is shooting straight down out of the originThe fourth line is shooting straight up out of the origin (any non-number means Vertical, not just Vertical)The fifth line is…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
- Create a program that: 1. Prompts the user to enter a string. 2. Use ANY method you'd like to remove all the vowels from the string (string function substring, accessing the string characters as arrays, etc.), creating a new string. 3. Output the new string with all the vowels removed. 4. The program MUST contain two functions-- (1) to remove all the vowels from the string, i.e., create a new string from the old string; (2) to determine if a character is a vowel. 5. Use an enumerated type to evaluate each character to determine if it is a vowel. Hint: Write the program WITHOUT the enumerated type first to get it working.arrow_forwardIn JAVA language: Define a function in which a string is passed as a parameter and the function returns true if the length of the string is a multiple of 3. Otherwise, the function returns false.arrow_forwardWrite a C++ program in Microsoft Visual Studio 2019, use only strings and functions. You need to create a game using strings. First generate a random letter from A to Z, this is your key and is hidden from the players. Ask player1 and player 2 to enter two strings of length 10. The player whose strings contains the key alphabet will win. If both the players have key alphabets, then the player for which it occurs earlier in the string will win. Sample Input: As computer generate a random letter such as Key: S (This Key must not be visible to user ) After that player1 enter this input in string. Player1: ABDXSCJMNK; And player2 enter this input in string Player2: CSTUZWKMIJ Sample output: Player 2 wins.arrow_forward
- Create a function which replaces all the x's in the string in the following ways: 1. Replace all x's with "cks" UNLESS: 2. The word begins with "x", therefore replace it with "z". 3. The word is just the letter "x", therefore replace it with "ecks". Examples xPronounce("Inside the box was a xylophone") → "Inside the bocks was a zylophone" xPronounce ("The x ray is excellent") "The ecks ray is eckscellent" xPronounce ("OMG x box unboxing video x D") → "OMG ecks bocks unbocksing video ecks D"arrow_forwardIn Python: Define a function in which a string is passed as a parameter and the function returns true if the length of the string is a multiple of 3. Otherwise, the function returns false.arrow_forwardİN C PROGRAMMİNG Write a function that takes a string as a parameter. The function should change the first letter of each word with the character '.'. You can assume that all characters in the string will be either a letter from the English alphabet or space. The spaces can be anywhere and in any number in the string.Example:If the parameter is : "This is just an example" then the function should change it to: ".his .s .ust .n .xample"If the parameter is: " This is another example " then the function should change it to: " .his .s .nother .xample "arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning