Let s1 Good and s2 Afternoon Write a C statement using str
Let s1 = \"Good\" and s2 = \"Afternoon\". Write a C++ statement using string manipulation functions to: Append the first 5 characters from the string in s2 to the string in s1. Compare the strings s1 and s2. Copy one string into the other (choose the appropriate order). Determine the length of the string in s1, and prmt the result.
Solution
Here are the solutions for you:
int main()
{
string s1 = \"Good\";
string s2 = \"Afternoon\";
//a. Append the first 5 characters from the string in s2 to the string in s1.
string temp = s1.append(s2, 0, 5);
cout<<temp<<endl;
//b. Compare the strings s1 and s2.
if(s1.compare(s2))
cout<<\"The strings are not equal.\"<<endl;
else
cout<<\"The strings are equal.\"<<endl;
//c. Copy one string into the other.
temp = s2; //Copies Afternoon to temp.
cout<<temp<<endl;
//d. Determine the length of the string in s1, and print the result.
int s1Length = s1.length();
cout<<\"The length of s1 is: \"<<s1Length<<endl;
}
