Hi! I need help with programing assignment. Code should be on C++. Here is full
ID: 3585417 • Letter: H
Question
Hi! I need help with programing assignment. Code should be on C++. Here is full requirements list. There should be 2 programs. Program A1 is shorter and program A2 contains several exercises. I simply haven't time this week so help me, please!
Problem A1
Write a program containing the following functions, in this order:
main - calls the other functions; otherwise does almost nothing
getSize - which asks the user how many strings they want
getSpace - which gets an array in the heap of the size requested by the user
inputData - which allows the user to input the strings and stores them in the array
printData - which prints all the strings, one string per line
destroy - which returns all the space to the heap
All of these functions, except main, shall have a return type of void.
Problem A2
Write ONE program containing all these string exercises.
Exercise 1
Read a string from the user into a string named string1.
Test it with the data: Good morning
Print the following heading and the data contained in string1:
Exercise 1
Good morning
Exercise 2
Create a new string named string2 containing the contents from string1.
Append the following text to the end of string 2.
sleepy heads
(Note that there is a space before sleepy.)
Print the following heading and the data contained in string2:
Exercise 2
Good morning sleepy heads
Exercise 3
Create string3 with an initial value of:
bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow
Find the location of the first h in string3
Print the following heading and the location found in string3:
Exercise 3
20
Exercise 4
Create an empty string named string4.
Copy string3 into string4.
Using the location found in exercise 3,
change the first h into J
Print the following heading and the data contained in string4:
Exercise 4
bellow cello fellow Jello mellow Novello Othello pillow Rollo solo yellow
Exercise 5
Create a copy of string3 named string5.
Use the find_last_of member function to find the position of the last l
Print the following heading and the position of the last l
Exercise 5
70
Exercise 6
Create a new string named string6 containing the contents from string3.
Using the position of the last l found in exercise 5, erase all characters from string6 following the last l
Print the following heading and the data contained in string6:
Exercise 6
bellow cello fellow hello mellow Novello Othello pillow Rollo solo yell
Explanation / Answer
Program 1:
#include <iostream>
#include <string>
using namespace std;
void getSize();
void getSpace();
void inputData();
void printData();
void destroy();
int size=0;
string* arrayString;
int main() {
getSize();
getSpace();
inputData();
printData();
destroy();
return 0;
}
void getSize(){
cout<<"Enter the size of the array"<<endl;
cin>>size;
}
void getSpace(){
arrayString =new string[size];
}
void inputData(){
for(int i=0;i<size;i++){
string currentElement;
cout<<"Enter "<<i+1<<" element :"<<endl;
cin>>currentElement;
arrayString[i]=currentElement;
}
}
void printData(){
cout<<"array of elements"<<endl;
for(int i=0;i<size;i++){
cout<<arrayString[i]<<endl;
}
}
void destroy(){
delete[] arrayString;
}
Program 2:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string string1="";
string string2="";
string string3="bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
string string4="";
string string5="";
string string6="";
cout<<"Enter String1:(Excercise1 input) "<<endl;
cin>>string1;
cout<<"Exercise 1 output "<<endl;
cout<<string1<<endl;
string2=string1+" sleepy heads";
cout<<"Exercise 2"<<endl;
cout<<string2<<endl;
cout<<"Exercise 3"<<endl;
cout<<string3.find("h")<<endl;
string4=string3;
string4=string4.replace(string3.find("h"),1,"j");
cout<<"Exercise 4"<<endl;
cout<<string4<<endl;
string5=string3;
cout<<"Exercise 5"<<endl;
cout<<string5.find_last_of("l")<<endl;
string6=string3;
string6=string6.replace(string5.find_last_of("l"),string6.length(),"");
cout<<"Exercise 6"<<endl;
cout<<string6<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.