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
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Concepts Of Programming Languages
Starting Out With Visual Basic (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- 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_forwardCreate 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_forward
- In C++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_forwardIn pythonarrow_forward
- capCount.py make a program called capCount.py that has a function that takes in a string and prints the number of capital letters in the first line, then prints the sum of their indices in the second line. The string "hEllo, World" would should look like this: 28arrow_forwardComplete the rotate_text() function that takes 2 parameters, a string data and an integer n. If n is positive, then the function will shift all the characters in data forward by n positions, with characters at the end of the string being moved to the start of the string. If n is 0 then the text remains the same. For example: rotate_text('abcde', rotate_text('abcde', rotate_text('abcde', 1) would return the string 'eabcd' 3) would return the string 'cdeab' 5) would return the string 'abcde' rotate_text('abcde', 6) would return the string 'eabcd' ... and so on. If n is negative, then the function will shift the characters in data backward by n positions, with characters at the start of the string being moved to the end of the string. For example: rotate text('abcde', -1) would return the string 'bcdea'arrow_forwardA spoonerism is when the first letters / sounds of two words are transposed onto one another. Create a function that takes a two-word string and performs a spoonerism on the phrase. Examples spoonerise ("history lecture") "listory hecture" spoonerise("loud noises") → "noud loises" spoonerise("chow mein") → "mow chein" spoonerise ("edabit rules!") Notes → "redabit ules!" • Only two words will be parsed into the function. Don't worry about handling more than two. • You won't always just have to swap the first letters, take care to notice which letters have been switched in the examples (notice the difference between vowel-starting and consonant-starting words).arrow_forward
- DescriptionA researcher is analyzing DNA. A DNA can be represented as a string composed of the characters A, G, C, or T.One day, researchers found a strange DNA, which is Smooth Repeated DNA. The DNA is represented by a string that has infinite length. The string has a repeating pattern, i.e. the DNA string 0 is repeated an infinite number of times. For example, if0 = "????", then = "???????????? . . . ".According to researchers, a DNA is said to be special if it contains substrings . Determine whetheris a substring of . Squad FormatA line containing the two strings 0 and . Output FormatA line that determines whether it is a substring of . Issue “YES” ifis a substring of . Output “NO” otherwise. Example Input and Output Input Example Example Output AGCT GC YES AGCT TA YES AGCT GT No AGCT TAGCTAGCT YES AGGACCTA CTAA YES Explanation ExampleIn the first to fourth test case examples, is worth "???????????? . . . ". The part in bold is one of the…arrow_forwardPython Previously, your check_move function took two separate arguments: a column and a row value. Now you will rewrite the function to accept the board position as a single string argument. In other words, write check_move (position) which takes position strings such as '85" that include both the column and the row value of a chess board. • When both the coordinates in position are valid, for example, "c4", the function returns 'The piece is moved to C4.'. • If position has too many or too few characters, return 'The position is not valid.' • If the first coordinate is out of range (regardless of whether the second one is or not) return 'The column value is not in the range a-h or A-HI¹. . Otherwise, if the second coordinate is out of range, return 'The row value is not in the range 1 to 81¹. Note that, irrespective of the casing of the column value, your code should output the value in upper case. Your function should work like this: >>> check_move('B4') 'The piece is moved to B4.'…arrow_forward1. In using scanf the reference to data was always passed. When using printf the reference was passed for a string, but the data value was required for types such as integer. This says that what is commonly refer to as a variable actually has 3 parts: 1 - The label (sometimes called the id or name); 2 - the reference; 3 - the value. Explain to the best of your understanding what is the difference between each of these three parts, and how it changes the cognitive model or metaphor you use to understand a variable. The type of the variable (how the variable is stored and operations on the variable) are associated with what part of the variable, the label, reference, or value? 2. Which parts of the variable exist at compile time, and which parts exist at run time? 3. Harder question with more insight: Can a value be a reference? Can you think of an example? (Think of structs and arrays). 4. Even harder, this is a real world example. I read that Python has immutable primitive types (e.g.…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning