Question

C++ Program Help Prompt your user to enter a length of a word to search from...

C++ Program Help
Prompt your user to enter a length of a word to search from a string. Use forking to spawn a child process to perform the counting task. Allow the child process to finish and output the number of words found (if any), before prompting the user for the next length. Count all words in the string with this length and output the number of words that correspond to this length. Program keeps asking user for length till user enters "0" which terminates the program

EXAMPLE:
String: Hello I am posting on Chegg Study!

Output:
Enter word length : 5
Count: 3
Enter word length : 2
Count: 2
Enter word length : 0
Program ended

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

// do comment if any problem arises

// code

#include <iostream>

#include <sys/wait.h>

#include <unistd.h>

using namespace std;

int main()

{

    // string to search for

    string temp = "Hello I am posting on Chegg Study!";

    while (true)

    {

        cout << "Enter word length: ";

        int length;

        // read length of word to search

        cin >> length;

        // exit condition

        if (length == 0)

            break;

        // fork a child

        if (fork() == 0)

        {

            // count is number of matches found

            int count = 0;

            // current length of current word while traversing string

            int current = 0;

            for (int i = 0; i < temp.length() + 1; i++)

            {

                // space or end of string

                if (temp[i] == ' ' || temp[i] == '\0')

                {

                    if (current == length)

                        count++;

                    current = 0;

                }

                else

                {

                    // ignore punctuations

                    if (temp[i] == ',' || temp[i] == '.' || temp[i] == '!')

                        continue;

                    else

                        current++;

                }

            }

            cout << "Count: " << count << endl;

            exit(0);

        }

        else

        {

            wait(NULL);

        }

    }

    cout << "Program ended\n";

}

Output:

Enter word length: 5 Count: 3 Enter word length: 2 Count: 2 Enter word length: 0 Program ended

Add a comment
Know the answer?
Add Answer to:
C++ Program Help Prompt your user to enter a length of a word to search from...
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
  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews...

  • write a python program a) The program will prompt the user to enter the length of...

    write a python program a) The program will prompt the user to enter the length of the two vertical sides. b) The length of the two horizontal sides will be twice as long as the length of the two vertical sides. c) The rectangle will be filled with the color green

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • write a program C++ that asks a user to enter a word from the following list...

    write a program C++ that asks a user to enter a word from the following list (turtle, tree, tin). If they entered the word turtle, tell them they picked an animal. If they entered the word tree, tell them they picked a plant. If they entered the word tin, tell them they picked a mineral. If they did not enter one of these words, output an error message.

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • Write a C program where user enter a string input, tokenizer the string using space character....

    Write a C program where user enter a string input, tokenizer the string using space character. Calculate the lengh of string. Find word start. Find word end. Count words.

  • Hello, I have a assignment where the user inputs a word and the out will then...

    Hello, I have a assignment where the user inputs a word and the out will then tell the user how many of the five vowels are in the word so for example if I typed the word hello, it would output {0 1 0 0 0 } since there is only one vowel and that is e which is in the second row, I have most of it done, but I can't seem to figure out how to get the...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...

    C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!...

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
ADVERTISEMENT