Question

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 keyboard. N is the number of items in the array upon entrance to the function and N should be the number of items remaining in the array upon returning from the function. It is also assumed that a[0] through a[N-1] contain valid items upon entering and a[0] through a[N-1] should still contain the same items upon returning from the function, except all occurrences of Search-Element have been removed. (e.g. You should not leave 'gaps' in the array when you remove Search-Element. For example Delete 6 from the array 3,4,6,2,1,6,8 with N=7. You could end up with, for example, the array 3, 4, 2, 1, 8, and N=5.


2. For the Count–Words option, write a C++ program that moves linearly through char data in an array b[ ] until a '\n' is reached. The program should count the total number of words in a sentence— typed in by the user—from keyboard and should output the result. A word is defined to be any contiguous group of ASCII chars that start with letters A through Z (upper case ) that don't contain white space. Words are separated by white space - e.g. blanks, or tabs. Of course, the last word could be terminated by the '\n' character. For example, if the array b[] contains: the Dog At5674 654 Mypaper'\n', your program should output: Total word count: 3. Words are: Dog, At5674, Mypaper.

3. For the Rev–Words option, create a C++ function, RevWordsInPlace, which has the prototype

void RevWordsInPlace(char array[]);

The function should reverse the words contained in array. The words in array are separated one from another by a space (' ') character, the first character of the first word is in array[0], and a newline ('\n') character immediately follows the last character of the last word. The reversal must be done in-place, that is, no local (temporary) arrays should be used. You may use appropriate local variables for counting and indexing purposes.

Examples

Array Before ..........................Array After

{one-word}.............................. {one-word}

{testing one two three}.............{three two one testing}

{Sam and Kate are happy}.......{happy are Kate and Sam}



4. If the user enters “Q” or “q” then you should verify that the user wants to terminate the program before actually terminating the program. If the user enters any other selection it should prompts the user invalid selection and tries again.

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

Program.cpp

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void Erase( int a[], int *N, int Search_Element )
{
    for(int i=0;i<*N;i++){
        if(a[i]==Search_Element){
            a[i]=NULL;
        }
    }
    int temp[*N],j=0;
     for(int i=0;i<*N;i++)
        temp[i]=a[i];
     for(int i=0;i<*N;i++){
          a[i]=NULL;
        if(temp[i]!=NULL){
            a[j]=temp[i];
            j++;
        }
    }
    *N=j;//final size of array
}
void RevWordsInPlace(char array[]){

string temp;
int j=0;

cout<<"\n\nArray Before\n\n"<<array;
cout<<"\n\nArray After\n\n";
for(int i=0;;i++){

     if(array[i]=='\n' || array[i]==NULL) {
    for(;j<i;j++){

           temp+=array[j];

    }
char t[temp.length()+1] ;
strcpy(t,temp.c_str());
temp="";
char *split;
string temp1;
split=strtok(t," ");
while(split != NULL){
temp1=split;
temp=temp1+" "+temp;

split=strtok(NULL," ");
}

cout<<temp<<endl;
temp="";
j=i;
     }
     if(array[i]==NULL)
        break;
}


}

int main()
{
    char ch;
    int N;

    do{
   cout<<"\n[E]rase–ArrayContent\n[C]ount–Words\n[R]ev–Words\n[Q]uit\n";
   cin>>ch;
   switch(ch){
       case 'E':
       {
           cout<<"\nHow many elements do you want to enter? ";
        cin>>N;
        int a[N];
        cout<<"\nEnter array elements ";
         for(int i=0;i<N;i++){
            cin>>a[i];
         }
       cout<<"\nEnter element you want to remove ";
       int Search_Element;
       cin>>Search_Element;
        Erase( a, &N, Search_Element );
        
        cout<<"\nContents of array are ";
         for(int i=0;i<N;i++){
              
                 cout<< a[i]<<" ";
         }
       }
       break;
       case 'C':
       {
           bool alpha=true,space=true;
           int words=0;
   string found_words;
         char sentence[1000]/*="Dog At5674 654 Mypaper"*/;
      
    cout<<"Enter the sentence ";
cin.ignore();
        cin.getline(sentence,sizeof(sentence));

         for(int i=0;;i++){
    
            if(sentence[i]=='\n' || sentence[i]==NULL){
                break;
            }
           if(space && isalpha(sentence[i])){
                words++;
                alpha=true;
                space=false;
                
                found_words+=",";
            }else{
                space=false;
            }
            if(sentence[i]==' '){
                
               alpha=false;
            space=true;    
            }
          
            if(alpha){
                found_words+=sentence[i];
            }
         }
         cout<<"\nTotal word count: "<<words<<". Words are:"<<found_words;
       }
       break;
       case 'R':
       {
           
           char sentence[]="one-word\ntesting one two three\nSam and Kate are happy";
           RevWordsInPlace(sentence);
       }
       break;
       
       case 'Q':
         cout<<"Do you really want to quit? (Y or N)";
         cin>>ch;
       break;

   }
}while(ch!='Y');
    return 0;
}

Output

user@ubuntu:$./a.out E]rase-ArrayContent [Clount-Words [R]ev-Words [Qluit How many elenents do you want to enter? 5 Enter array elenents 6 5 5 Enter elenent you want to renove S Contents of array are 6 4 4 [E]rase-ArrayContent [Clount-Words [R]ev-Words [Qluit Enter the sentence Dog At5674 654 Mypaper Total word count: 3. Words are: ,Dog,At5674,Mypaper E]rase-ArrayContent [Clount-Words [R]ev-Words

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

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

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • 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"

  • Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items...

    Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items in an array (a non-nega- tive integer), and saves it in an int variable called numltems. It then prompts user for the values of all the items (non-negative integers) and saves them in an int array called items. The program shall then print the contents of the array in a graphical form, with the array index and values represented by number of stars For...

  • 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...

  • 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...

  • 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...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • 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