Question

Use c-strings for the following project: Write a C++ program that declares an array containing up...

Use c-strings for the following project:

Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered.

Then, one by one, display each sentence entered by the user and present the following menu of operations on each sentence:

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

If option 1 is selected, the program calls a function which takes the string and a character and returns either the index within the string of the first occurrence of the character or -1 if it could not be found. Print the location it was first found or that it was not found. If found, print how many times it was found.

If option 2 is selected, the program calls a function that takes a string, an integer for the position of the character to be changed and a character and changes the character located at the given position in the string to the given character. Keep looping until a valid value is entered for the position.

If option 3 is selected, a function is called which takes a string and an integer n and displays the first n characters of the string. Keep looping until a valid value is entered for the number of characters to be printed.

If option 4 is selected, a function is called which takes a string and an integer n and displays the last n characters of the string. Keep looping until a valid value is entered for the number of characters to be printed.

If option 5 is selected, a function is called which takes a string and two integers and displays all the characters that lie between the two indices, inclusive. Keep looping until a valid value is entered for both numbers.

If option 6 is selected, a function is invoked which takes a string and nulls the string meaning it makes it a 0-length string. If it already has 0 length, it must display a message saying that.

If option 7 is selected at any time, the program exits the loop and displays all the sentences entered, including any modification done by the user.

If the user has not existed and all sentences have been displayed and processed, the program must display all the sentences again, including any changes that might have been made to them by the user.

Upload just the cpp file using the Browse and Upload buttons. (The cpp file is in the folder that has the name you gave the project.

The following is a Sample execution of the program:

Enter up to 20 sentences - when done, Press ENTER:

1. My roommate is not home right now.

2. I am a student at Valley College.

3. My brother is not home.

4. Programming in C++ is fun.

5. My sister has a new job.

6. Valley Glen, CA, is a residential area.

7. I'm hungry; Let's order a pizza.

8. I only have $51.32 on me.

9. My brother Joe just got married.

10. He lives in Ohio.

11. This is just another simple string.

12. [ENTER]

Here is sentence 1:

My roommate is not home right now.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 1

Please enter the character to search for: o

There are 5 o's in the sentence, with the 1st one at index 4.

Here is sentence 2:

I am a student at Valley College.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 1

Please enter the character to search for: v

There are no v's in "I am a student at Valley College."

Here is sentence 3:

My brother is not home.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 2

What is the index of the character you want to change? 21

What character do you want in that position? w

The new string is "My brother is now home"

Here is sentence 4:

Programming in C++ is fun.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 2

What is the index of the character you want to change? 30

What character do you want in that position? X

That position is outside the bounds of the string. String unchanged.

Here is sentence 5:

My sister has a new job.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 3

How many characters from the beginning of the string do you want to display? 8

My broth

Here is sentence 6:

Valley Glen, CA, is a residential area.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 3

How many characters from the beginning of the string do you want to display? 32

Error: too many characters.

Here is sentence 7:

I'm hungry; Let's order a pizza.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 4

How many characters from the end of the string do you want to display? 10

r a pizza.

Here is sentence 8:

I only have $51.32 on me.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 4

How many characters from the end of the string do you want to display? 30

Error: too many characters.

Here is sentence 9:

My brother just got married.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 5

Please enter the beginning index: 5

Please enter the ending index: 12

other Ju

Here is sentence 10:

He lives in Ohio.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 5

Please enter the beginning index: 5

Please enter the ending index: 32

Error: one or more indices are outside the bounds of the array.

Here is sentence 11:

This is just another simple string.

What do you want to do?

1. Search for a character in the sentence

2. Change a character within the sentence

3. Display the first n characters of the sentence

4. Display the last n characters of the sentence

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

Choice: 6

The string is now Null.

My roommate is not home right now.

I am a student at Valley College.

My brother is now home.

Programming in C++ is fun.

My sister has a new job.

Valley Glen, CA, is a residential area.

I'm hungry; Let's order a pizza.

I only have $51.32 on me.

My brother Joe just got married.

He lives in Ohio.

Press any key to continue . . .

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

Code

#include<iostream>
#include<string.h>

using namespace std;
#define NUM_STRINGS 20
#define NUM_CHAR 81
int getSentences(char [][NUM_CHAR]);
int printMenu();
int findChar(char [],char);
void changeCharcter(char [],char,int);
void displayFirstNChar(char [],int);
void displayLastNChar(char [],int);
void displayCharBetweenIndices(char [],int,int);
void nullTheSentence(char []);
int main()
{
   char sentences[NUM_STRINGS][NUM_CHAR];
   int number_of_sentence;
   int choice,pos;
   char ch;

   number_of_sentence=getSentences(sentences);
   memset(&sentences[number_of_sentence], 0, sizeof(sentences[number_of_sentence]));
   for(int i=0;i<number_of_sentence;i++)
   {
       cout<<"Here is sentence "<<(i+1)<<":"<<endl;
       cout<<sentences[i]<<endl;
       choice=printMenu();
       switch (choice)
       {
       case 1:
           cout<<"Please enter the character to search for: ";
           cin>>ch;
           pos=findChar(sentences[i],ch);
           if(pos>0)
               cout<<"with the 1st one at index "<<pos<<".\n";
           break;
       case 2:
           cout<<"What is the index of the character you want to change?";
           cin>>pos;
           cout<<"What character do you want in that position?";
           cin>>ch;
           changeCharcter(sentences[i],ch,pos);
           break;
       case 3:
           cout<<"How many characters from the beginning of the string do you want to display?";
           cin>>pos;
           displayFirstNChar(sentences[i],pos);
           break;
       case 4:
           cout<<"How many characters from the end of the string do you want to display?";
           cin>>pos;
           displayLastNChar(sentences[i],pos);
           break;
       case 5:
           int startPos,endPos;
           cout<<"Please enter the beginning index: ";
           cin>>startPos;
           cout<<"Please enter the ending index: ";
           cin>>endPos;
           displayCharBetweenIndices(sentences[i],startPos,endPos);
           break;
       case 6:
           nullTheSentence(sentences[i]);
           break;
       case 7:
           break;
       default:
           cout<<"Invalid choice."<<endl;
           break;
       }
   }
   cout<<"\n\n";
   int i=0;
   while(true)
   {
       int len=strlen(sentences[i]);
       if(len==0)
           break;
       i++;
       cout<<sentences[i]<<endl;
   }
   return 1;
}
int printMenu()
{
   int choice;
   cout<<"What do you want to do?"<<endl;
   cout<<"1. Search for a character in the sentence"<<endl;
   cout<<"2. Change a character within the sentence"<<endl;
   cout<<"3. Display the first n characters of the sentence"<<endl;
   cout<<"4. Display the last n characters of the sentence"<<endl;
   cout<<"5. Display all characters that lie between two given indices"<<endl;
   cout<<"6. Null the string"<<endl;
   cout<<"7. Exit"<<endl;
   cout<<"Choice: ";
   cin>>choice;
   return choice;
}
int getSentences(char sen [][NUM_CHAR])
{
   char input[81];

   int count=0;
   cout<<"Enter up to 20 sentences - when done, Press ENTER:"<<endl;
   while(count<NUM_STRINGS)
   {
       cout<<(count+1)<<". ";
       gets(input);
       if(strcmp(input,"")==0)
           break;
       strcpy(sen[count],input);
       count++;
   }
   return count;
}

int findChar(char sen[],char c)
{
   int count=0;
   bool firstFind=true;
   int index;
   for(int i=0;sen[i]!='\0';i++)
   {
       if(sen[i]==c)
       {
           if(firstFind)
           {
               index=i;
               firstFind=false;
           }
           count++;
       }
   }
   if(count==0)
   {
       cout<<"There are no "<<c<<"'s in \""<<sen<<"\""<<endl;
           return -1;
   }
   cout<<"There are "<<count<<" "<<c<<"'s in the sentence, ";
   return index;
}
void changeCharcter(char sen[],char c,int pos)
{
   int len=strlen(sen);
   if(pos>=0 && pos<len)
   {
       sen[pos]=c;
       cout<<"The new string is \""<<sen<<"\""<<endl;
       return;
   }
   cout<<"That position is outside the bounds of the string. String unchanged."<<endl;
}
void displayFirstNChar(char sen[],int pos)
{
   int len=strlen(sen);
   if(pos<len)
   {
       for(int i=0;i<pos;i++)
           cout<<sen[i];
       cout<<endl;
       return;
   }
   cout<<"Error: too many characters."<<endl;
}

void displayLastNChar(char sen[],int pos)
{
   int len=strlen(sen);
   if(pos<len)
   {
       for(int i=len-pos;i<len;i++)
           cout<<sen[i];
       cout<<endl;
       return;
   }
   cout<<"Error: too many characters."<<endl;
}
void displayCharBetweenIndices(char sen[],int start,int end)
{
   int len=strlen(sen);
   if(start>=0 && start<len && end>start && end<len)
   {
       for(int i=start;i<end;i++)
           cout<<sen[i];
       cout<<endl;
       return;
   }
   cout<<"Error: one or more indices are outside the bounds of the array."<<endl;
}
void nullTheSentence(char sen[])
{
   int len=strlen(sen);
   if(len!=0)
   {
       memset(&sen, 0, sizeof(sen));
       cout<<"The string is now Null."<<endl;
       return;
   }
   cout<<"String is already null"<<endl;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Use c-strings for the following project: Write a C++ program that declares an array containing up...
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
  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • In C Programming: (100 pts) Write a program to find the words that have a specific...

    In C Programming: (100 pts) Write a program to find the words that have a specific character among the words of a string. Read a string and a character from the user and display all the words having that character on the screen. One way to do this is to find the words in one sentence and check if they have the required character in them. Another way is to find all the occurence of the character and then find...

  • Three C Code Questions: 5. Write a C program that declares an array of 10 strings,...

    Three C Code Questions: 5. Write a C program that declares an array of 10 strings, each of max length 50 characters, and then reads an entire line of text from the keyboard into each string. 6. Write C code that finds the longest name in this array of strings that you read in Q5. Hint: find the position of the longest string, not the string itself. 7. Write a C program that creates a 4 x 6 two dimensional...

  • Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has...

    Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has its own set of rules you need to use to decode it. You will use String methods to change the messages you receive from the user. *Note: Ensure that you make the changes in the order they are specified and do the operations on the updated String Sentence 1: Make everything after the first word lowercase Remove the last space Remove the first 3...

  • Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts...

    Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts the user to enter a sentence, the program then does the following: 1. Prints the total characters in the sentence. 2. Prints the word count of the sentence, assuming only one space between each tow words. 3. Prints total vowel (a, e, i, o, u) in the sentence. 4. Prints the vowel frequency as shown in the sample below. 5. Prints the sentence in...

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

  • Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version...

    Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version of this file (hw6-Spring2017.txt) and searches thru the file for the first occurrence of each of the 26 uppercase characters of the alphabet (A-Z), then each the 10 digits (0-9), and finally each of the 26 lowercase characters of the alphabet (a-z). As it finds each of these characters it should also record its position in the file (assume the first character in the...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

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