COMP1511 Revision Characters Sample Solutions

Revision Exercise: Character Numbers

Computers encode characters (letters, numbers, and symbols) as numbers. For this exercise we'll assume the ASCII encoding is used. As a side note, ASCII is a subset of the most widely used modern character encoding: UTF-8

In ASCII the integers (0-31) represent control characters, which don't print anything. Similarly, the integer 127 represents another special character, the delete character. The integers 32 to 126 are printing characters - for example the integer 42 represents an asterisk ('*') character.

Create a C program called character_numbers.c which prints the integers 32 to 126 in decimal and hexadecimal with the the character they represent in ASCII.

Match the example output below EXACTLY:

dcc character_numbers.c -o character_numbers
./character_numbers
 32 0x20
 33 0x21 !
 34 0x22 "
 35 0x23 #
 36 0x24 $
 37 0x25 %
 38 0x26 &
 39 0x27 '
 40 0x28 (
 41 0x29 )
 42 0x2a *
 43 0x2b +
 44 0x2c ,
 45 0x2d -
 46 0x2e .
 47 0x2f /
 48 0x30 0
 49 0x31 1
 50 0x32 2
 51 0x33 3
 52 0x34 4
 53 0x35 5
 54 0x36 6
 55 0x37 7
 56 0x38 8
 57 0x39 9
 58 0x3a :
 59 0x3b ;
 60 0x3c <
 61 0x3d =
 62 0x3e >
 63 0x3f ?
 64 0x40 @
 65 0x41 A
 66 0x42 B
 67 0x43 C
 68 0x44 D
 69 0x45 E
 70 0x46 F
 71 0x47 G
 72 0x48 H
 73 0x49 I
 74 0x4a J
 75 0x4b K
 76 0x4c L
 77 0x4d M
 78 0x4e N
 79 0x4f O
 80 0x50 P
 81 0x51 Q
 82 0x52 R
 83 0x53 S
 84 0x54 T
 85 0x55 U
 86 0x56 V
 87 0x57 W
 88 0x58 X
 89 0x59 Y
 90 0x5a Z
 91 0x5b [
 92 0x5c \
 93 0x5d ]
 94 0x5e ^
 95 0x5f _
 96 0x60 `
 97 0x61 a
 98 0x62 b
 99 0x63 c
100 0x64 d
101 0x65 e
102 0x66 f
103 0x67 g
104 0x68 h
105 0x69 i
106 0x6a j
107 0x6b k
108 0x6c l
109 0x6d m
110 0x6e n
111 0x6f o
112 0x70 p
113 0x71 q
114 0x72 r
115 0x73 s
116 0x74 t
117 0x75 u
118 0x76 v
119 0x77 w
120 0x78 x
121 0x79 y
122 0x7a z
123 0x7b {
124 0x7c |
125 0x7d }
126 0x7e ~

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest character_numbers
Sample solution for character_numbers.c
// Print out an ASCII table of "character numbers".

#include <stdio.h>

int main(int argc, char *argv[]) {
    int i = 32;
    while (i < 127) {
        printf("%3d 0x%x %c\n", i, i, i);
        i = i + 1;
    }
    return 0;
}

Revision Exercise: Parrot

Write a program parrot.c that uses getchar() and putchar() to echo the user's input.

getchar() reads the next character (byte) from standard input and returns it. If getchar() is unable to read a character it will return the special value EOF defined in stdio.h. This will happen for example when reading from a file when the end of the file is reached.

You can simulate the end of a file being reached on a Linux (and Unix-like) terminal by holding the Ctrlkey and pressing the D key - This will cause getchar() to return the special EOF value.

putchar() takes a single character (byte) as a parameter and prints it to standard output.

For example:

dcc parrot.c -o parrot
./parrot
abc
abc
ABCabc123
ABCabc123
abc
abc
xyz
xyz

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest parrot
Sample solution for parrot.c
// Read characters until eof
// Andrew Taylor - andrewt@unsw.edu.au
// 22/4/2018

#include <stdio.h>

int main(void) {
    // getchar returns an int which will contain either
    // the ASCII code of the character (if input is ASCII) or EOF

    int ch = getchar();
    while (ch != EOF) {
        putchar(ch);
        ch = getchar();
    }
    return 0;
}

Revision Exercise: Text To Int

You should make sure you have completed parrot before completing this task.

Write a program text_to_int.c which reads characters and prints their decimal values, each on a new line.

Copy your parrot.c file and use it as a starting point.

For example:

dcc text_to_int.c -o text_to_int
./text_to_int
abc
97
98
99
10
17
49
55
10
Hello, world!
72
101
108
108
111
44
32
119
111
114
108
100
33
10
17 is my favourite number.
49
55
32
105
115
32
109
121
32
102
97
118
111
117
114
105
116
101
32
110
117
109
98
101
114
46
10

Note in the above example the decimal value 10 is the new line character.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest text_to_int
Sample solution for text_to_int.c
#include <stdio.h>

int main(int argc, char *argv[]) {
    int ch = getchar();
    while (ch != EOF) {
        printf("%d\n", ch);
        ch = getchar();
    }
    return 0;
}

Revision Exercise: Lowercase

Download lowercase.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/lowercase/lowercase.c .

Your task is to add code to this function in lowercase.c:

// Returns the specified character `c`, in lowercase.
int lowercase(int c) {
    // Your code goes here!
    // Don't forget to return your result.
    return 0;
}
The function lowercase takes in a character, c and if it is an uppercase letter, converts it to lowercase and returns that value. All other input values should be returned unchanged.

Here is how lowercase.c should behave after you add the correct code to the function lowercase:

dcc lowercase.c -o lowercase
./lowercase
ABC
abc
ABCabc123
abcabc123
123!@#
123!@#

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest lowercase
Sample solution for lowercase.c
// Print out characters in lower case.
// A sample solution.

#include <stdio.h>

int lowercase(int c);

int main(int argc, char *argv[]) {

    // Scan in the character.
    int c = getchar();

    // Loop until there are no more characters to scan.
    while (c != EOF) {
        // Print the character, in lowercase.
        putchar(lowercase(c));

        // Get the next character.
        c = getchar();
    }

    return 0;
}

// The difference between lowercase and uppercase letters.
// If we had the letter 'A', and we added _something_ to it, we would
// get 'a' -- as the ascii values for lowercase letters are higher than
// the ascii values for uppercase letters.
//
// The _something_ that we have to add is the difference between lower
// and uppercase letters.
// e.g. 'A' + ('a' - 'A') can be rearranged to 'A' - 'A' + 'a',
// which gives us lowercase 'a'.

int lowercase(int c) {
    if (c >= 'A' && c <= 'Z') {
        return c - 'A' + 'a';
    } else {
        return c;
    }
}

Revision Exercise: Uppercase

Download uppercase.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/uppercase/uppercase.c .

Your task is to add code to this function in uppercase.c:

int uppercase(int c) {
    // PUT YOUR CODE HERE

    return 0; // change to your return value
}
The function uppercase takes in a character, c and if it is an uppercase letter, converts it to uppercase and returns that value. All other input values should be returned unchanged.

Here is how uppercase.c should behave after you add the correct code to the function uppercase:

dcc uppercase.c -o uppercase
./uppercase
abc
ABC
ABCabc123
ABCABC123
123!@#
123!@#

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest uppercase
Sample solution for uppercase.c
// alex linker 2017-08-17
// main function for uppercase activity

#include <stdio.h>

int uppercase(int c);

int main(int argc, char *argv[]) {

    // get the character
    int c = getchar();

    // loop until end of characters
    while (c != EOF) {
        // print the character in uppercase
        putchar(uppercase(c));

        //get the next character
        c = getchar();
    }

    return 0;
}

int uppercase(int c) {
    if (c >= 'a' && c <= 'z') {
        return c - 'a' + 'A';
    } else {
        return c;
    }
}

Revision Exercise: Rotate One

Download rotate_one.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/rotate_one/rotate_one.c .

Your task is to add code to this function in rotate_one.c:

// "Rotate" the letter by one, i.e. change 'a' to 'b', 'b' to 'c',
// 'z' to 'a'.
int rotateOne(int c) {
    // Your code goes here!
    // Don't forget to return your result.
    return 0;
}
The rotate_one function should "rotate" a letter through the alphabet by one position.

This means that if the input is 'a', the output should be 'b'. 'z' becomes 'a' and 'Z' becomes 'A'.

Any non-letter characters should be returned unchanged.

Here is how rotate_one.c should behave after you add the correct code to the function rotate_one:

dcc rotate_one.c -o rotate_one
./rotate_one
Hello, world!
Ifmmp, xpsme!
17 is my favourite number.
17 jt nz gbwpvsjuf ovncfs.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest rotate_one
Sample solution for rotate_one.c
// Rotate a character by one -- i.e. turn 'a' to 'b', 'b' to 'c', ...
// 'z' to 'a'.
// A sample solution.

#include <stdio.h>

char rotateOne(char c);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {

    // Scan in the first character.
    int c = getchar();

    // Loop until there are no more characters to scan.
    while (c != EOF) {
        // Print the character, "rotated" once.
        putchar(rotateOne(c));

        // Get the next character.
        c = getchar();
    }

    return 0;
}
// END OF MAIN FUNCTION

// "Rotate" the letter by one, i.e. change 'a' to 'b', 'b' to 'c',
// ..., 'z' to 'a'.
char rotateOne(char c) {
    // If the character is between 'a' and 'y', we can just add one
    // to it.
    if ((c >= 'a' && c <= 'y') || (c >= 'A' && c <= 'Y')) {
        c = c + 1;
    } else if (c == 'z') {
        // If it's 'z', we can't just add one -- we need it to wrap
        // around.
        c = 'a';
    } else if (c == 'Z') {
        // Similarly for capital 'Z'.
        c = 'A';
    }

    return c;
}