Question

In: Computer Science

We will write a simple program that simply uses the three standard library functions. The following...

  1. We will write a simple program that simply uses the three standard library functions. The following is a high level description of the main program (main.cpp). In this program, please do the following:
    • Include the C-string library. #include <cstring>
    • Declare the following C-style string: char line[] = "ls -l -a | wc -c >> myfile";.
    • Declare a variable of type size_t named "len".
    • Use the builtin strlen function to compute the length of the variable "line". Store the result in "len".
    • Output the value of "len" ("The length of 'line' is..."). (The answer should be 26 for this string.)
    • Create an array of 10 C-strings named "strArray". (char* strArray[10];)
    • Initialize each element of "strArray" to be a string containing just the null character, "\0". Note that there is a difference between the null character, '\0', and a string containing only the null character, "\0". The former is a character, just like 'a' or '{'. The latter is a string that is functionally empty, e.g. "".
    • Use the builtin strtok function to split "line" into pieces using a space as the delimiter. Each piece will be placed into "strArray". Here is an overview of how that should work: 1) Declare an integer named "pieces" and set it to 0 initially. 2) Create a char* named "ptr" and assign it the return value of strtok, where "line" and " " are the two arguments. 3) Create a while loop that iterates while "ptr" is not NULL. 4) Set "strArray[pieces]" equal to "ptr". 5) Set "ptr" to the return value of strtok, where NULL and " " are the two arguments. 6) Increment "pieces".
    • Using "pieces" as the upper index, print out each element of "strArray", one per line. E.g. "strArray[0] = ...". Notice that we have split the original "line" variable into pieces. strArray[0] should be "ls", strArray[1] should be "-l", and so on.
    • Create a size_t variable named "size", and initialize it to 0.
    • Again using "pieces" as the upper index, calculate the sum of the lengths of each string in "strArray". Put the result in "size".
    • Create a size_t variable named "numBlanks". See if you can initialize it to the number of spaces in "line". HINT: You can do it using information you already have. You don't need another loop!
    • Print out the value of "numBlanks". (E.g. "The number of blanks is ..."). You can check for yourself that the answer should be 7 for the string we gave you.
    • Our last task will be to concatenate the elements of "strArray" into one big string using strcat. First, create a C-string that is large enough to hold the result. (char* result = new char[size + 1];)
    • Set result[0] to the null character, '\0'.
    • Once more, using "pieces" as the upper bound, call strcat successively with "result" as the first argument and strArray[i] as the second.
    • Print out the contents of "result" (e.g. "The concatenation of all strings in strArray is: ...").
    • Finally, clean up after yourself by entering "delete[] result;" Remember, for each new, there MUST BE a matching delete.

Solutions

Expert Solution

Solution:

#include<iostream>

#include<cstring>

using namespace std;

int main()

{

    //Declaring C style string

    char line[]="ls -l -a | wc -c >> myfile";

    size_t len;

    //Finding length of string 'line'

    len=strlen(line);

    //Printing the length of string

    cout<<"The length of 'line' is "<<len<<endl;

    //Declaring an array of C-strings

    char* strArray[10];

    int i=0;

    //Initializing each string to \0

    while(i<10)

    {

        strArray[i]=(char *)"\0";   

        i++;

    }

    int pieces;

    pieces=0;

    char* ptr;

    //breaking line into tokens based on delimiter " "

    ptr=strtok(line," ");

    while(ptr!=NULL)

    {

        strArray[pieces]=ptr;

        ptr=strtok(NULL," ");

        pieces=pieces+1;

    }

    //Printing contents of strArray

    for(i=0;i<pieces;i++)

        cout<<"strArray["<<i<<"] = "<<strArray[i]<<endl;

    //Finding sum of lengths of all strings in strArray

    size_t size=0;

    for(i=0;i<pieces;i++)

        size=size+strlen(strArray[i]);

    

    //Finding number of blanks in line

    size_t numBlanks=pieces-1;

    cout<<"The number of blanks is "<<numBlanks<<endl;

    char* result = new char[size+1];

    result[0]='\0';

    //Concatenating all strings in strArray into a big string

    for(i=0;i<pieces;i++)

        strcat(result,strArray[i]);

    

    //Printing the result

    cout<<"The concatenation of all strings in strArray is: "<<result;

    delete[] result;

    return 0;

}

Output:


Related Solutions

You will need to write a library of functions that uses pointers. Your functions must follow...
You will need to write a library of functions that uses pointers. Your functions must follow the headers below. int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr); Create a function to do integer division that gives the division result and remainder via output parameters, quo_optr and rem_ptr.   Additionally, the function must return an int representing the success/failure of the function. The function should return 0 if it succeeds and -1 if there is a divide by zero error. (Note...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
c++ Write a program that displays the status of an order. a) Program uses 2 functions...
c++ Write a program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display...
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. 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...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Look up the man pages for the following string functions from the C standard library, and...
Look up the man pages for the following string functions from the C standard library, and write implementations of each: strstr() Please explain the work please, thank you
what is the following (ARDUINO): -Examples of a standard Arduino library -The Arduino uses a structured...
what is the following (ARDUINO): -Examples of a standard Arduino library -The Arduino uses a structured programming language-what type -Channels of A/D conversion on the Arduino Uno -The Arduino correlates temperature readings with what
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT