DPST1091 Revision Characters Sample Solutions
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
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: Lowercase
Download lowercase.c here, or copy it to your CSE account using the following command:
cp -n /import/reed/A/dp1091/public_html/24T3/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;
}
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
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 /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
}
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
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: Swap the case of letters in a string. (●●◌)
Download swap_case.c here, or copy it to your CSE account using the following command:
cp -n /import/reed/A/dp1091/public_html/24T3/activities/swap_case/swap_case.c .
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
swap_case.c
// Written 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// Write stdin to stdout with upper case letters converted to lower case
// and lower case converted to upper case
//
#include <stdio.h>
#include <stdlib.h>
int swap_case(int character);
int main(int argc, char *argv[]) {
int character = getchar();
while (character != EOF) {
int swapped_character = swap_case(character);
putchar(swapped_character);
character = getchar();
}
return 0;
}
int swap_case(int character) {
if (character >= 'A' && character <= 'Z') {
return 'a' + character - 'A';
} else if (character >= 'a' && character <= 'z') {
return 'A' + character - 'a';
} else {
return character;
}
}