in C++. Thanks Problem 1.2 Write one program containing all these string exercis
ID: 3709687 • Letter: I
Question
in C++. Thanks
Problem 1.2
Write one program containing all these string exercises.
Exercise 1.2.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 1.2.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 1.2.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
The first h is at column (put the column number here)
Exercise 1.2.4
Create an empty string named string4.
Copy string3 into string4.
Using the location found in exercise 3,
change the first h into a 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 1.2.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 y
Exercise 5
(put the column number of the last y here)
Exercise 1.2.6
Create a new string named string6 containing the contents from string3.
Using the position of the last y found in exercise 5, erase all
characters from string6 following the last y
Print the following heading and the data contained in string6:
Exercise 6
bellow cello fellow hello mellow Novello Othello pillow Rollo solo y
Explanation / Answer
Exercise 1.2.1
#include<iostream>
using namespace std;
int main()
{
string string1 = "Good morning";
cout<<"Exercise 1 ";
cout<<string1;
return 0;
}
Exercise 1.2.2:
#include<iostream>
using namespace std;
int main()
{
string string1 = "Good morning";
cout<<"Exercise 2 ";
string string2 = string1;
string2 += " sleepy heads";
cout<<string2;
return 0;
}
Exercise 1.2.3
#include<iostream>
using namespace std;
int main()
{
string string3 = "bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
int i;
for( i = 0 ; i < string3.length() ; i++ )
{
if( string3[i] == 'h' )
{
cout<<"The first h is at column "<<i;
break;
}
}
return 0;
}
Exercise 1.2.4
#include<iostream>
using namespace std;
int main()
{
string string3 = "bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
string string4 = string3;
int i;
cout<<"Exercise 4 ";
for( i = 0 ; i < string3.length() ; i++ )
{
if( string3[i] == 'j' )
{
cout<<"The first j is at column "<<i;
break;
}
}
cout<<string4;
return 0;
}
Exercise 1.2.5
#include<iostream>
using namespace std;
int main()
{
string string3 = "bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
string string5 = string3;
int i, index = -1;
cout<<"Exercise 5 ";
for( i = 0 ; i < string5.length() ; i++ )
{
if( string3[i] == 'y' )
{
index = i;
}
}
cout<<index;
return 0;
}
Exercise 1.2.6
#include<iostream>
using namespace std;
int main()
{
string string3 = "bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
string string6 = string3;
int i, index = -1;
cout<<"Exercise 6 ";
string temp = "";
for( i = 0 ; i < string6.length() ; i++ )
{
if( string3[i] == 'y' )
{
index = i;
}
}
for( i = 0 ; i <= index ; i++ )
temp += string6[i];
string6 = temp;
cout<<string6;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.