UECS1643/UECS1653 Fundamentals of Programming
1
Practical 9
–
refer to Topics 17 and 18
Part A (Understanding Concepts)
1.
Given the program below, what is the output if the input is the following?
(a)
5
(b)
P
(c)
(Tab key)
(d)
g
(e) ?
#include <iostream>
#include <cctype> // for character manipulation functions
using namespace std;
int main(void)
{
char ch;
do
{
cout << "Enter a character (press <enter> to stop): ";
fflush(stdin);
ch = getchar();
if (isalnum(ch))
{
cout << "Alphanumeric character\n";
if (isalpha(ch))
{
if (islower(ch))
cout << "Lowercase letter\n";
else if (isupper(ch))
cout << "Uppercase letter\n";
}
else if (isdigit(ch))
cout << "Digit\n";
}
else if (isspace(ch))
cout << "Whitespace\n";
else
cout << "Some other character\n";
} while (ch != '\n');
return 0;
}

UECS1643/UECS1653 Fundamentals of Programming
2
2.
Given the program below, what is the output if the input is the following?
(a)
h
(b)
Y
(c)
0
#include <iostream>
#include <cctype> // for character manipulation functions
using namespace std;
int main(void)
{
char ch_in, ch_out;
do
{
cout << "Enter a character (type 0 to stop): ";
cin >> ch_in;
ch_out = tolower(ch_in);
cout << "tolower of " << ch_in << " returns " << ch_out << endl;
ch_out = toupper(ch_in);
cout << "toupper of " << ch_in << " returns " << ch_out << endl;
} while(ch_in != '0');
return 0;
}
3.
What is the output of the following program?
#include <iostream>
#include <cstring> // for string manipulation functions
using namespace std;
int main(void)
{
char s1[15], s2[5];
strcpy(s1, "good");
strcpy(s2, " job");
cout << "s1:" << s1
<< endl;
cout << "s2:" << s2
<< endl;
cout << "s1 length:" <<
strlen(s1) << endl;
cout << "s2 length:" <<
strlen(s2) << endl;
cout << "s2 from 2nd element:" << &s2[1] << endl;
strcat(s1, s2);
cout << "s1:" << s1 << endl;
strncat(s1, s2, 3);
cout << "s1:" << s1 << endl;
return 0;
}
