Question

   Create   a   C-string   variable   that   contains   a   name,   age,   and   title.   Each   field   is separat

   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.

Solution must be in C++ Language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstring>

using namespace std;

int main() {

        char *data = "Bob 45 Programmer";

        char name[50];
        char age[50];
        char title[50];

        // copy name.
        int i, j;
        for(i=0; i<strlen(data); i++) {
                if(data[i] == ' ') {
                        name[i] = '\0';
                        break;
                }
                name[i] = data[i];
        }
        i++;
        j=0;
        for(; i<strlen(data); i++) {
                if(data[i] == ' ') {
                        age[j] = '\0';
                        break;
                }
                age[j++] = data[i];
        }
        i++;
        j=0;
        for(; i<strlen(data); i++) {
                if(data[i] == ' ') {
                        title[j] = '\0';
                        break;
                }
                title[j++] = data[i];
        }

        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
        cout << "Title: " << title << endl;


}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
   Create   a   C-string   variable   that   contains   a   name,   age,   and   title.   Each   field   is separat
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python code. No use of def() or return. 7a)Create a program that asks the user for...

    Python code. No use of def() or return. 7a)Create a program that asks the user for their full name (first, middle, and last). You can assume that compound names, such as Jamie-Lynn are joined by a hyphen, that names are separated by a space and that the user enters their data in the correct format. The program should then separate the first name, middle name, and last name from the single string entered by the user and save each piece...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Write a python function score_formatter() that takes one parameter, which is a formatted string that contains...

    Write a python function score_formatter() that takes one parameter, which is a formatted string that contains information about the score received by a student in a particular subject. The function analyzes the string and returns a reformatted string for the score. You will need to use the function re.sub that is discussed in the lecture notes. If the string does not match the pattern, the function returns the string "error". The input string is always given in the format specified...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string"...

    Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string" is a sequence of characters. In the C language anything within double quotes is a "string constant" so you have been seeing strings all semester. But we can also have string variables. In the C language these are implemented as an array of char, e.g. char name (10]: In order to make these variables easier to work with, it has been universally agreed that...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • Create a program that presents a menu with these options: Add employee Print Employee Report Quit...

    Create a program that presents a menu with these options: Add employee Print Employee Report Quit Read the initial employee list from a text file and store the employee information into a DICTIONARY called empDict. Key empID, value is a LIST that contains name, payrate, and hours. The text file will be structured as shown below, where each line contains the employee id, employee name, payrate and hours. Each entry on the line is separated by a comma and a...

  • Part 2: Processing Strings (Individual work) Processing user-entered data to follow a specific format is a...

    Part 2: Processing Strings (Individual work) Processing user-entered data to follow a specific format is a common task. Often this involves using string functions to manipulate a set of input strings. Create a MATLAB script named namephone.m and place these lines of code at the beginning: name  = input('Enter your first and last name: ','s'); phone = input('Enter your area code and phone number: ','s'); Tasks Here are useful string functions:  length, strcat, strtrim, lower, upper, strcmp, findstr, strrep As you work...

  • In C program Consider a file that contains employee records, called "empstat.txt." The data for each...

    In C program Consider a file that contains employee records, called "empstat.txt." The data for each employee consist of the employee’s last name (up to 20 characters), employee id number (up to 11 characters), gross pay for the week (double), taxes deducted (double) for the week, and net pay (double) for the week. Create a struct to hold the employee information. Write a void function called writeEmpFile that prompts the user to enter last name, id number, gross pay and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT