CISC105 Fall 2006 Lab08
CISC105 Fall 2006 Lab08
- Write a program or webpage for each of the following problems. Be sure to
save every separate program. All programs must be properly commented
and indented (see Assignment Standards on the class website).
- Remember that I take exam questions from labs. Alway try to
figure out the concept behind each lab problem, and see me if you
can't figure it out.
- Name each program lab08.n.c, where n is the number in the list
below. For example, the name of the file for the first will be
lab08.1.c. Put the files in your lab08 directory.
Programs
-
Create the functions myStrcpy and myStrcat. In your
program, create some sample strings (what is a string in C?) and
demonstrate the C function strcpy, followed by myStrcpy, then the
function strcat followed by myStrcat. Remember that all C string
handling functions are written with a sentinel loop.
- The C string function strcmp compares two strings. However, it
does not return true when they are equal; its behavior is more
complex. Because type char elements can be treated as integers,
strcmp traverses both arrays being compared, subtracting the
elements (one char from another) as it goes (and it goes until it
hits a what?).
As long as the result of subtraction is zero, it knows the letters
are the same and it keeps going. If two letters are different, it
stops and returns a result with the same sign1 as the result of the
subtraction.
In your program, call strcmp on "apple" and "doughnut", then on
"doughnut" and "apple" to see which way the sign goes. Then write
myStrcmp and demonstrate that it returns the same sign as strcmp for
those cases and: apple and applet, bat and bat, bats and bat.
- Create a four row, four column array of char and read four
words in from the user: tea, party, biscuit, roll. After all four words have been stored, print
them out. Annotate your script by hand and briefly explain what prints out.
- Copy the previous program and reverse the loop, i.e. if you had
looped from 0 to 3, now loop from 3 to 0. After all four words have been stored, print
them out. Annotate your script by hand and briefly explain what prints out.
- Create two data files, with a different number ( < 50) of
integers in each. Declare two arrays in main(), size 50. Write a
function that will read all of the integers (use the feof() function
from class) from a file into an array. Call the same function twice,
once for each data file. The size of the data files must not appear
anywhere in your program.
- Write a program to read in a single char from the user. Then use
fgets(), as shown in class, to be sure your program isn't confused by extra input. Use
a switch statement to have your program print a (real) word starting with
the letter entered by the user, but any non-alpha character should print
an error message. Lower and upper case letters should be treated the
same (hint: see 9.6 of H&K).
Your main should not be longer than ten lines.
- Write a program that will print all the words
entered on the command line by the user, not including the name of
the program.
-
Write a program to read in two ints and a double from the
command line. Use a function from the beginning of Appendix B in
H&K to convert the string representation of the numbers to int or
double. Have your program print the highest of the three. Your
program should not perform more than two numeric comparisons to find
the max.
You should have a total of 8 programs named lab08.1.c to
lab08.8.c. Make a single script file (see lab00 for the
instructions) where you cat, compile, and run each one in its final form.
Submit all C files and your script on WebCT, and give the paper version of the
complete script file only to your TA
at the beginning of your next lab (all Friday labs) or in lecture Friday
(Wednesday labs only). Note: Cat, compile, and run each
program in order! Do not cat all programs, then compile, etc.
Footnotes:
1or even the
same number, depending on the implementation
File translated from
TEX
by
TTH,
version 3.38.
On 29 Oct 2006, 12:11.