Description
DeVry COMP 122 Week 7 Homework Latest
Week 7 Homework
Part 1: Complete the following problems.
- 1.Answer the questions that follow given these lines of code.
string str1, str2;
cin >> str1 >> str2;
if(str1 == str2)
{
cout << str1 << ” == ” << str2 << endl;
}
else if (str1 > str2)
{
cout << str1 << ” > ” << str2 << endl;
}
else
{
cout << str1 << ” < ” str2 << endl;
}
- a.What do these lines output if the input is “diamond diamond”?
- b.What do these lines output if the input is “diamond gold”?
- c.What do these lines output if the input is “silver gold”?
- 2.What is the output of the following program fragment?
string s1 = “Ball Park”;
string s2 = “Going to”;
string s3 = “the”;
string str;
cout << s2 << ” ” << s3 << ” ” << s1 << endl;
cout << s1.length() << endl;
cout << s1.find(‘P’) << endl;
cout << s1.substr(0, 4) << endl;
str = “abcdefghijk”;
cout << str << endl;
cout << str.length() << endl;
str[0] = ‘A’;
str[3] = ‘D’;
cout << str << endl;
- 3.What is output from each of the following statements? Assume each statement is independent of all the others.
string str = “Now is the time for the party!”;
- a.cout << str.size() << endl;
- b.cout << str.substr(7, 8) << endl;
- c.cout << str.insert(11, “best “) << endl;
- d.str.erase(16, 14);
str.insert(16, “to study for the exam!”);
cout << str << endl;
Part 2: Complete the following problems.
- 4.Given the following declaration: char str[16];
mark the following statements as valid or invalid and if invalid, explain why.
- a.strcpy(str, “Hello there”);
- b.strlen(str);
- c.str = “Jacksonville”;
- d.cin >> str;
- e.cout << str;
- f.if ( str >= “Nice guy”)
cout << str;
- g.str[6] = ‘t’;
- 5.Given the following declarations:
char s1[15];
char s2[15] = “Good Day!”;
Write C statements to do the following. Note: No loops allowed!
- a.Copy s2 to s1.
- b.Check if s1 and s2 contain the same string and output a message if they are equal.
- c.If s1 is less than s2, output s1.Otherwise output s2.
- d.Store the string “Sunny Day” into s1.
- e.Store the length of s2 into an int variable named length.
- 6.Given the following declarations:
char name[21] = “Bob”;
char yourName[21] = “Joe”;
char studentName[31] = “Joe Bob”;
Mark each of the following as valid or invalid. If invalid, explain why.
- a.cin >> name;
- b.cout << studentName;
- c.yourName[0] = ‘