1 |
h13 |
CS16 S17 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h13: Chapter 8: Strings
ready? | assigned | due | points |
---|---|---|---|
true | Wed 05/24 02:00PM | Wed 05/31 02:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)
Please:
- No Staples.
- No Paperclips.
- No folded down corners.
Read Chapter 8 and the lecture notes. If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”.
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL! FOR BEST RESULTS, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.
1.(2 pts) How are ordinary arrays of characters and c-strings similar and how are they dissimilar?
2.(4 pts) What are two (2) things that are wrong with this use of a c-string (ignore why someone would write this code, focus on logic/syntax errors)?
char s1[5] = "Mark", s2[5] = "Jill";
for (int i = 0; i <= 5; i++)
s1[i] = s2[i];
if (s1 != s2) s1 = "Art";
3.(4 pts) What is the output of the following code?
char s1[4] = "abc", s2[4] = "ABC";
if (strcmp(s1, s2)) cout << "Hi!";
else cout << "Hey!";
4.(8 pts) The following code takes in a string input from the user and performs an integer multiplication, as seen in the example run here. Note that the input string will contain the asterix character (i.e. *):
Enter 2 integer numbers to be multiplied, like this: num1*num2: 15*3
The answer is: 45
Complete the missing code below that performs this task (it can be done in 2 lines, but you can use more if you like).
string s; int k(0);
cout << "Enter 2 integer numbers to be multiplied, like this: num1*num2: ";
cin >> s;
cout << "The answer is: " << k << endl;
5.(10 pts) Show the output produced when the following code (entire program not shown) is executed. You are encouraged to also try to compile this in a program to verify your results.
string name = "Jeffery Tambor";
cout << "NAME = " + name << endl;
cout << name.length() << endl;
name.erase(8, 6);
cout << name << endl;
name.append("Dean WD Morgan");
cout << name << endl;
name.insert(22, "@TWD");
cout << name << endl;
name.replace(23, 3, "The WD");
cout << name << endl;
cout << name.find("WD") << endl;
cout << name.rfind("WD") << endl;
cout << name.rfind("fery") << endl;
for (int i = name.length(); i > 20; i--)
cout << name[i-1];
cout << endl;