CISC105 Fall 2006 Lab08 CISC105 Fall 2006 Lab08

Programs

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Write a program that will print all the words entered on the command line by the user, not including the name of the program.

  8. 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.