#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size
// uninitialized array elements
int main()
{
int array[SIZE]; // uninitialized array of integers
int i;
unsigned n; // array size
printf("Give the array size (0 <= n <= %d): ", SIZE);
scanf("%u", &n);
if (n > SIZE)
{
printf("0 <= n <= %d\n", SIZE);
printf("Your input: n == %u\n", n);
return 1; // signal error
}
printf("You have %d item%s:\n", n, n == 1 ? "" : "s");
for (i = 0; i < n; i++)
{ // print values of uninitialized array
printf("%x%c", array[i], (i % 10 == 9 || i == n-1) ? '\n' : ' ');
} // %x hexadecimal
for (i = 0; i < n; i++)
{ // print addresses:
printf("%p%c", &array[i], (i % 5 == 4 || i == n-1) ? '\n' : ' ');
}
return 0;
}
/*
gcc items.c -o items
./items
Give the array size (0 <= n <= 100): -1
0 <= n <= 100
Your input: n == 4294967295
./items
Give the array size (0 <= n <= 100): 101
0 <= n <= 100
Your input: n == 101
./items
Give the array size (0 <= n <= 100): 0
You have 0 items:
./items
Give the array size (0 <= n <= 100): 1
You have 1 item:
0
0x7ffdb4d52c10
./items
Give the array size (0 <= n <= 100): 7
You have 7 items:
0 20 0 0 0 0 0
0x7ffcc11f3600 0x7ffcc11f3604 0x7ffcc11f3608 0x7ffcc11f360c 0x7ffcc11f3610
0x7ffcc11f3614 0x7ffcc11f3618
./items
Give the array size (0 <= n <= 100): 100
You have 100 items:
0 20 0 0 0 0 0 0 1 0
3ae75f6 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 a4531fe8 7fff 0 0
0 0 0 0 0 0 0 0 0 0
53ad8040 5573 f0b6ff 0 c2
0 a452eed7 7fff a452eed6 7fff
............................. // 100 addresses
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-10. Rewrite the function lower(), which converts upper case letters to lower case, with a conditional expression instead of if-else.
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
void lower(char []); // to lower case
void upper(char []); // to upper case
int main()
{
char s[LENGTH];
printf("Enter a word in lower case: ");
scanf("%s", s); upper(s);
printf("%s\n", s);
printf("Enter a word in upper case: ");
scanf("%s", s); lower(s);
printf("%s\n", s);
return 0;
}
void lower(char s[]) // to lower case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
(s[i] >= 'A' && s[i] <= 'Z') ?
s[i] = (s[i] + 'a' - 'A') : 1; // 'a' > 'A' in ASCII
} // compiler requires an expression after : (1 is an expression)
}
void upper(char s[]) // to upper case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
(s[i] >= 'a' && s[i] <= 'z') ?
s[i] = (s[i] - 'a' + 'A') : 0; // s[i] - ('a' - 'A')
} // compiler requires an expression after : (0 is an expression)
}
/*
gcc case1.c -o case1
./case1
Enter a word in lower case: Hello,
HELLO,
Enter a word in upper case: World!
world!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
int lower (int c); // char to lower case
int upper (int c); // char to upper case
void lowers(char []); // string to lower case
void uppers(char []); // string to upper case
// inefficient variant (too many function calls)
int main()
{
char s[LENGTH];
printf("Enter a word in lower case: ");
scanf("%s", s); uppers(s);
printf("%s\n", s);
printf("Enter a word in upper case: ");
scanf("%s", s); lowers(s);
printf("%s\n", s);
return 0;
}
int lower (int c) // char to lower case
{
return (c >= 'A' && c <= 'Z') ?
(c + 'a' - 'A') : c; // 'a' > 'A' in ASCII
}
int upper (int c) // char to upper case
{
return (c >= 'a' && c <= 'z') ?
(c - 'a' + 'A') : c; // c - ('a' - 'A')
}
void lowers(char s[]) // string to lower case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
s[i] = lower(s[i]);
}
}
void uppers(char s[]) // string to upper case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
s[i] = upper(s[i]);
}
}
/*
gcc case2.c -o case2
./case2
Enter a word in lower case: Hello,
HELLO,
Enter a word in upper case: World!
world!
*/