DPST1091 Revision Chars Exercises

Revision Exercise: Parrot

Write a program parrot.c that uses scanf("%c", ...) and printf("%c", ...) to echo the user's input.

Your program should repeat this until the user enters

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:

1091 autotest parrot

Revision Exercise: Lowercase

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

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

1091 autotest lowercase

Revision Exercise: Uppercase

Download uppercase.c here

Or, copy these file(s) to your CSE account using the following command:

1091 fetch-activity uppercase

Your task is to add code to these functions 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

Revision Exercise: Swap the case of letters in a string. (●●◌)

Download swap_case.c here

Or, copy these file(s) to your CSE account using the following command:

1091 fetch-activity swap_case

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

int swap_case(int character) {
    // TODO: Write this function, which should:
    //  - return character in lower case if it is an upper case letter
    //  - return character in upper case if it is an lower case letter
    //  - return the character unchanged otherwise

    return 'x';
}

Edit the C program swap_case.c (linked above) which reads characters from its input and writes the same characters to its output with lower case letters converted to upper case and upper case letters converted to lower case.

Your program should stop only at the end of input.

which:

  • returns the character in lowercase if it is an uppercase letter
  • returns the character in uppercase if it is a lowercase letter
  • returns the character unchanged otherwise

Note: Your program will not pass autotests if it does not contain this function.

Examples

dcc swap_case.c -o swap_case
./swap_case
Are you saying 'Boo' or 'Boo-Urns'?
aRE YOU SAYING 'bOO' OR 'bOO-uRNS'?
In this house, we obey the laws of thermodynamics!
iN THIS HOUSE, WE OBEY THE LAWS OF THERMODYNAMICS!
UPPER !@#$% lower
upper !@#$% LOWER

Assumptions/Restrictions/Clarifications

  • You need only a single int variable. Don't use an array.
  • Make sure you understand this example program which reads characters until end of input.
  • Make sure you understand this example program which reads characters, printing them with lower case letters converted to uppercase.

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

1091 autotest swap_case