Question

In: Computer Science

Write a C++ function called parse that reads one line of user input from the keyboard...

Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function.

For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”.

Create a main function that has a loop that will continuously call the function until the user enters the string “quit”.  After the function has returned control to main, print the contents of the array and its length.

Your program should:

Give detailed directions to the user.

  • Contain carefully worded pre and post conditions for the parse function.
  • Be readable with appropriate documentation and formatting.
  • Use an array of cstrings.
  • Read the user’s input one character at a time using cin.get().
    • You will need to build the strings one character at time using white space to separate the strings.  (Remember that cstrings end in \0.)
    • DO NOT USE getline( ) !!!
  • Skip any extra whitespace within the users input or at the end of the input
    • copy this that and    copy       this          that      should produce the same result.

Solutions

Expert Solution

Here is the complete code for the following problem. I have added all required comments in program as well as pre and post conditions for the method .

And also display the content of array at last to verify that out program is working fine.

Code:

#include <iostream>
#include <cstring>

using namespace std;


//pre-condition <-- an array of type string and and variable of type int
//post-condition <-- an array with number of elements equal to array length

//method to add a new string in array
void parse(string arr[], int& arr_len)
{
char c ;
int space_count = 0; //use to monitor if user gives more than one space between words
//loop call the parse function untill user enter quit
//and read char by char from user
while(cin.get(c))
{
if( c != ' ') //if the next char is space
{
space_count = 0;
  
if(arr[arr_len] == "quit") //check if last entered string is space and if yes then break loop
break;
  
arr[arr_len] += c; //otherwise add it into array untill get next space
}
else if( c == ' ' && space_count ==0)
{
arr_len++;
space_count++;
}
}
}

int main()
{
//create two variable one for storing word in array
//and second count num of elements in array
string arr[40];
int arr_len = 0;
  
  
//call the parse method
parse(arr, arr_len);
  
//print the array to verify result
for(int i=0; i<arr_len; i++)
{
cout << arr[i] << " ";
}
return 0;
}

-----------------------------------------------------------------------------

Output:


Related Solutions

Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT