aboutsummaryrefslogtreecommitdiff
path: root/ch2/2-10_lower.c
blob: fe0288bf28594334a5406f5f5ae32167a380d540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

/* The C Programming Language: 2nd Edition
 *
 * Exercise 2-10: Rewrite the function 'lower', which converts upper case
 * letters to lower case, with a conditional expression instead of if-else.
 *
 * Answer: The tertiary ?: operators also _evaluate_, so they can be used in
 * a lot of different places.
 */

int lower(int c) {
	return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
}

int main() {
	char foo = 'F';
	printf("The follow letter should be lowercase: %c\n", lower(foo));
	return 0;
}