In this Lab, you will practise:
Create a new directory for this lab called lab09
by typing:
mkdir lab09Change to this directory by typing:
cd lab09
one.c
, which reads in a string from standard input and writes out the characters
one per line.
The output from your program should look like this:
./one Enter a string: Hello H e l l o
Hint: don't use scanf, use fgets to read the string.
Note, your program needs to read only one string - it doesn't have to read until the end of input.
You can assume lines contain at most 4096 characters.
As usual autotest is available to test your program.
~cs1511/bin/autotest lab09 one.c
Write a program, palindrome.c
, which reads a string and tests if
it is a palindrome.
For example:
./palindrome Enter a string: kayak String is a palindrome ./palindrome Enter a string: canoe String is not a palindromeHint: don't use scanf, use fgets to read the string.
Note, your program needs to read only one string - it doesn't have to read until the end of input.
You can assume lines contain at most 4096 characters.
As usual autotest is available to test your program.
~cs1511/bin/autotest lab09 palindrome.c
numbers.c
which takes 3 arguments which
creates a file containing specified integers, one per line. The first & second arguments
will specify a range of integers. The third argument will specify the file
to be created. For example:
./numbers 40 42 fortytwo.txt cat fortytwo.txt 40 41 42 ./numbers 1 5 a.txt cat a.txt 1 2 3 4 5 ./numbers 1 1000 1000.txt wc 1000.txt 1000 1000 3893 1000.txtYou should print a suitable error message if given the wrong number of arguments or if the file can not be created. As usual autotest is available to test your program.
~cs1511/bin/autotest lab09 numbers.c
head.c
, which given a filename as argument prints the first 10 lines
of the file. If the file has less than 10 lines the entire file should be printed.
Note unlike the 2 previous exercises this program should not create any files. Its just prints part of the content of a file to standard output.
For example:
./head 1000.txt 1 2 3 4 5 6 7 8 9 10 ./head /usr/include/stdio.h /* Define ISO C stdio on top of C++ iostreams. Copyright (C) 1991-2016 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful,It should also be possible to specify that a different number of lines be printed. This will specified by passing the string "-n" as the first argument to the program, the number of lines to be printed as the second argument and the file as the third argument. For example:
./head -n 3 1000.txt 1 2 3You should print a suitable message if incorrect arguments are supplied or the file can not be read.
You can assume lines have at most 1024 characters, but if possible avoid this assumption.
As usual autotest is available to test your program.
~cs1511/bin/autotest lab09 head.c
palindrome1.c
) so characters which are not letters
are ignored and difference between upper case and
lower case are ignored.
For example:
./palindrome1 Enter a string: Do geese see God? String is a palindrome ./palindrome1 Enter a string: Do ducks see God? String is not a palindrome ./palindrome1 Enter a string: Madam, I'm Adam String is a palindrome ./palindrome1 Enter a string: Madam, I'm Andrew String is not a palindrome
Hint: if you #include <ctype.h>
you can use a C library function named tolower
to convert a character to lower case.
~cs1511/bin/autotest lab09 palindrome1.c
tail.c
, which given a filename as argument prints the last 10 lines
of the file. If the file has less than 10 lines the entire file should be printed.
You can assume lines have at most 1024 characters, but you can not make any assumption about how many lines are in the file.
You can not read the entire file into an array.
For example:
./tail 1000.txt 991 992 993 994 995 996 997 998 999 1000 ./numbers 1 100000 100000.txt wc 100000.txt 100000 100000 588895 100000.txt ./tail 100000.txt 99991 99992 99993 99994 99995 99996 99997 99998 99999 100000
You should print a suitable message if incorrect arguments are supplied or the file can not be read.
Unlike the previous exercise it does not have to be possible to specify a different number of lines to be printed.
As usual autotest is available to test your program.
~cs1511/bin/autotest lab09 tail.c
lab09
directory):
give cs1511 lab09 one.c palindrome.c numbers.c head.c palindrome1.c tail.cSubmit the challenge exercises only if you attempt them.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember the lab assessment guidelines - if you don't finish the exercises
you can finish them in your own time, submit them
by Monday 11:00am using give
and ask your tutor to assess them at the start of
the following lab.
Either or both members of a programming pair can submit the work (make sure each program lists both of you as authors in the header comment).