Question

Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-A
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the screenshots of code & sample run output:

Below is the C++ program for the same. All the requirements mentioned in question are implemented.

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
/**function to Remove occurrences of Search/element from given array**/
void Erase(int a[],int *N, int *SearchElement){
int temp[*N];
int count=0;
for(int i=0;i<*N;i++)
if(a[i] != (*SearchElement))
temp[count++]=a[i];/**copying required values to temporary intermediate array**/
*N=count;/**updating array size**/
for(int i=0;i<*N;i++)
a[i]=temp[i];/**copying required values from temporary intermediate array**/
a = new int[*N];/**resizing array**/
delete temp;/**deleting temporary intermediate array**/
}
/**function to count words in a given string line**/
void count_words(char b[]){
cout<<endl;
int words = 0;/**word counter**/
vector<int> start;/**marks start of words**/
vector<int> end;/**marks end of words**/
bool delimiter = true;/**keeping track of encountering delimiter**/
for(int i=0;i<strlen(b);i++){
if(b[i]=='\n')/**if newline encountered then stop**/
break;
if(delimiter && b[i]>='A' && b[i]<='Z'){/**if uppercase letter encountered after a delimiter**/
start.push_back(i);/**add it to start of words**/
delimiter = false;
}
else if(!delimiter&&((b[i]=='\n')||(b[i]==' '))){/**if a new delimiter encountered**/
delimiter = true;/**add it to end of words**/
end.push_back(i);
words++;
}
}
if(!delimiter)/**if the last char was not a delimiter**/
words++;/**then increment word count**/
/**display results**/
cout<<"Total word count: "<<words<<".\nWords are: ";
end.push_back(strlen(b));
for(int i = 0; i < start.size(); i++){
for(int k=start[i];k<end[i];k++)
cout<<b[k];
cout<<((i<start.size()-1)?", ":".\n");
}
}
int main(){
while(true){
cout<<"MENU"<<endl<<"Erase-ArrayContent[Enter E]\tCount-Words[Enter W]\tQuit[Enter Q]\n";
cout<<"\nYour choice: ";/**display menu & prompt for choice**/
char ch;cin>>ch;
ch=tolower(ch);
while(ch!='e'&&ch!='w'&&ch!='q'){/**if invalid choice try again**/
cout<<"Invalid selection! Try again :";
cin>>ch;
}
if(ch=='e'){/**if erase option selected**/
cout<<"Enter the number of elements initially in array(N): ";
int N;cin>>N;/**input no. of elements**/
int a[N];
cout<<"Enter the "<<N<<" elements of array: ";
for(int i=0;i<N;i++)
cin>>a[i];/**input array**/
cout<<"Enter the search element to be erased: ";
int key;cin>>key;/**input search element**/
cout<<"\nErasing...\n";
Erase(a,&N,&key);/**erasing**/
/**printing updated details**/
cout<<"Number of elements(N) after erasing "<<" = "<<N<<endl;
cout<<"The modified array: [";
for(int i=0;i<N;i++)
cout<<a[i]<<((i==N-1)?"]\n":", ");
}
else if (ch=='w'){/**if option word count selected**/
cout<<"Enter the data for char array: "<<endl;
char b[100];
fflush(stdin);gets(b);/**input char array**/
count_words(b);/**count words**/
}
else{/**else if quit option selected, simply stop**/
cout<<"Do you really Want to quit? Enter q to confirm: ";
cin>>ch;
if(tolower(ch)=='q'){
cout<<"quiting...."<<endl;
break;
}
}
cout<<endl<<"-------------------------------------------------------------"<<endl;
}
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...
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
  • Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words...

    Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words [Q]uit 1. For Erase–ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase is as follows: void Erase( int a[ ], int * N, int * Search-Element ) The function Erase should remove all occurrences of Search-Element from the array    a[ ]. Note that array a[ ] is loaded with integer numbers entered by the user through the...

  • c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence,...

    c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence, then counts and prints the number of words in the sentence. Assume that there is more than one space between words of the sentence. Sample input/output: Enter a sentence: This is a 123test in There are 5 words in " This is a 123test \n"

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Please help C++ language Instructions Write a program that prompts the user to enter 50 integers...

    Please help C++ language Instructions Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs.

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