In this Lab, you will practise:
Create a new directory for this lab called lab07
by typing:
mkdir lab07Change to this directory by typing:
cd lab07
Some of the exercises in the labs require you to read characters using the getchar function.
getchar reads the next character from standard input and returns it.
If getchar is unable to read a character it return the special value EOF
.
When input is coming from a file, getchar will return EOF
after the last character in the file is read.
When input is coming from a (Linux/OSX) terminal, you can indicate no more characters
can be read by typing Ctrl+D.
This will cause getchar to return EOF
In some of this week's lab exercises you will find it convenient to put the test input in a file, rather than type it every time you want to test the program.
You can use a < character to indicate to the shell that you want to run a program taking its input from a file.
So for example you might create the file input.txt
with gedit and
then run a.out
takes its input from the file rather the terminal:
gedit input.txt & $ ./a.out <input.txt
strlen.c
which reads characters from stdin and prints the number of characters in that string. Most of the program is complete for you, you simply need to complete the strlength function.// Author: Hayden Smith 2019 #include#define MAXLINE 1024 int strlength(char *); int main(void) { char line[MAXLINE]; fgets(line, MAXLINE, stdin); printf("Strlen is %d\n", strlength(line)); } int strlength(char *s) { // TODO return 0; }
$ ./strlen cs1911 6
$ 1911 autotest lab07 strlen.c
devowel.c
which reads characters from its input
and writes the same charcters to its output, except it does not write lower case vowels ('a', 'e','i', 'o', 'u').
Your program should stop only at the end of input.
For example:
$ ./devowel The quick brown fox jumped over the lazy dog. Th qck brwn fx jmpd vr th lzy dg. It was the best of times. It was the worst of times. It ws th bst f tms. It ws th wrst f tms.
The only functions you are allowed to use are getchar
and putchar
.
Don't use printf
, scanf
or fgets
.
As usual autotest is available to test your program.
$ 1911 autotest lab07 devowel.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 palindrome
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.
$ 1911 autotest lab07 palindrome.c
one.c
, which reads in one string from the user and writes out the characters
one per line to a new file in the same directory called data.txt.
The output from your program should look like this:
$ ./one Enter a string: HelloThen when you try and print the file out you expect this:
H e l l o
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.
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.
$ 1911 autotest lab07 palindrome1.c
black.c
from the course account to your
directory by typing:
cp ~cs1911/public_html/23T2/code/black.c chessboard.cCompile it, run it and then type
display black.bmp
At the moment all you should see is a 512x512 completely black bitmap image.
Modify the file so it generates a chessboard image consisting of 16x16 black and white squares. The name of the bmp file it generates should be chessboard.bmp
Hint: You should only need to modify the generateData function and change the name of the BMP_FILE. Hint: Look at the example code (Wee7 lecture code) to create different colours and a digaonal bmpDemo.c.
Note: There is no autotest for this exercise. You must demonstrate your program by displaying the image it produces.lab07
directory):
give cs1911 lab07 strlen.c devowel.c one.c palindrome.c palindrome1.c chessboard.cSubmit advanced exercises only if you attempt the advanced exercise.
Remember the lab assessment guidelines - if you don't finish the exercises
you can finish them in your own time, submit them
by 19:59:59 Sunday using give
and ask ask tutor to assess them at the start of
the following lab.
You can also just run the autotests without submitting by typing
1911 autotest lab07