Week 04 Extra Sample Solutions

Information

  • This page contains extra exercises for week 04.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●◌◌)
:

Upper case

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

cp -n /import/reed/A/dp1091/public_html/24T3/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:

1091 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;
    }
}