wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Chapter 10: Characters, C-Strings, and string Class

Total questions: 51

Worksheet time: 1hrs 12mins

Name
Class
Date
1.

What header file must you include in a program using character testing functions such as isalpha and isdigit?

a)

#include <cstring>

b)

#include <string>

c)

#include <cctype>

d)

#exclude <ccbyte>

2.

What header file must you include in a program using the character conversion functions toupper and tolower?

a)

#exclude <cctype>

b)

#include <cctype>

c)

#input <cctype>

d)

#output <cctype>

3.

Assume c is a char variable. What value does c hold after each of the following statements executes?

c = toupper('a'); ​ (a)  

c = toupper('B'); ​ (b)  

c = tolower('D'); ​ (c)  

c = toupper('e'); ​ (d)  

Choose from the below words
A
B
d
E
a
b
D
e
4.

Look at the following code. What value will be stored in s after the code executes?

char name[10];

int s;

strcpy(name, "Jimmy");

s = strlen(name);

4 lines
5.

What header file must you include in a program using string functions such as strlen and strcpy ? ​ ​ (a)  

Choose from the below words
cctype
string
cstring
algorithm
char
iostream
iomanip
6.

What header file must you include in a program using string/numeric conversion functions such as atoi and atof ?

a)

#include <string>

b)

#include <cstring>

c)

#input <cstring>

d)

#input <string>

7.

What header file must you include in a program using string class objects?​

a)

#include <cctype>

b)

#include <cstring>

c)

#exclude <string>

d)

#include <string>

8.

How do you compare string class objects?

a)

mystring.compare(str);

b)

strcmp(str1, str2);

c)

strstr(str);

d)

mystring.append(str);

9.

The (a)   function returns true if the character argument is uppercase.

10.

The (a)   function returns true if the character argument is a letter of the alphabet.

11.

The (a)   function returns true if the character argument is a digit.

12.

The (a)   function returns true if the character argument is a whitespace character.

13.

The (a)   function returns the uppercase equivalent of its character argument.

14.

The (a)   function returns the lowercase equivalent of its character argument.

15.

The (a)   file must be included in a program that uses character testing functions.

16.

The (a)   function returns the length of a string.

17.

To (a)   two strings means to append one string to the other

18.

The (a)   function concatenates two strings.

19.

The (a)   function copies one string to another

20.

The (a)   function searches for a string inside of another one.

21.

The (a)   function compares two strings.

22.

The (a)   function copies, at most, n number of characters from one string to another

23.

The (a)   function returns the value of a string converted to an integer.

24.

The (a)   function returns the value of a string converted to a long integer.

25.

The (a)   function returns the value of a string converted to a float.

26.

The (a)   function converts an integer to a string.

27.

The following if statement determines whether choice is equal to ‘Y’ or ‘y’.

if (choice == 'Y' || choice == 'y')

Simplify this statement by using either the toupper or tolower function.

4 lines
28.

Assume input is a char array holding a C-string. Write code that counts the number of elements in the array that contain an alphabetic character.

4 lines
29.

Look at the following array definition.

char str[10];

Assume that name is also a char array, and it holds a C-string. Write code that copies the contents of name to str if the C-string in name is not too big to fit in str.

4 lines
30.

Look at the following statements.

char str[] = "237.89";

double value;

Write a statement that converts the string in str to a double and stores the result in value.

4 lines
31.

Write a function that accepts a pointer to a C-string as its argument. The function

should count the number of times the character ‘w’ occurs in the argument and return

that number.

4 lines
32.

Assume that str1 and str2 are string class objects. Write code that displays “They are

the same!” if the two objects contain the same string.

4 lines
33.

Character testing functions, such as isupper , accept strings as arguments and test each character in the string.

a)

True

b)

False

34.

If toupper ’s argument is already uppercase, it is returned as is, with no changes.

a)

True

b)

False

35.

If tolower ’s argument is already lowercase, it will be inadvertently converted to uppercase.

a)

True

b)

False

36.

The strlen function returns the size of the array containing a string.

a)

True

b)

False

37.

If the starting address of a C-string is passed into a pointer parameter, it can be assumed that all the characters, from that address up to the byte that holds the null terminator, are part of the string.

a)

True

b)

False

38.

C-string handling functions accept as arguments pointers to strings (array names or pointer variables), or literal strings.

a)

True

b)

False

39.

The strcat function checks to make sure the first string is large enough to hold both strings before performing the concatenation.

a)

True

b)

False

40.

The strcpy function will overwrite the contents of its first string argument.

a)

True

b)

False

41.

The strcpy function performs no bounds checking on the first argument.

a)

True

b)

False

42.

There is no difference between “847” and 847.

a)

True

b)

False

43.

Each of the following programs or program segments has errors. Find as many as you can.

char str[] = "Stop";

if (isupper(str) == "STOP")

exit(0);

4 lines
44.

Each of the following programs or program segments has errors. Find as many as you can.

char numeric[5];

int x = 123;

numeric = atoi(x);

4 lines
45.

Each of the following programs or program segments has errors. Find as many as you can.

char string1[] = "Billy";

char string2[] = " Bob Jones";

strcat(string1, string2);

4 lines
46.

Each of the following programs or program segments has errors. Find as many as you can.

char x = 'a', y = 'a';

if (strcmp(x, y) == 0)

exit(0);

4 lines
47.

Organize these options into the right categories

Categorize the following

isupper

isalpha

isspace

tolower

strlen

strstr

strcat

strcpy

mystring.at(x);

mystring.size();

mystring.append(str);

mystring.assign(str);

Character Functions
Cstring Functions
String Functions
48.

Organize these options into the right categories

Categorize the following

islower

isalnum

isdigit

ispunct

strncpy

strncat

strlen

strstr

mystring.begin();

mystring.end();

mystring.front();

mystring.back();

Character Functions
Cstring Functions
String Functions
49.

Which of the following is a character functions: ​ (a)   ​ (b)  

Choose from the below words
isupper
tolower
strlen
mystring.compare(str);
mystring.at(x);
strstr
50.

Which of the following is a cstring functions: ​ (a)   ​ (b)  

Choose from the below words
strcat
strcpy
mystring.compare(str);
mystring.at(x);
toupper
isdigit
51.

Which of the following is a string functions: ​ (a)   ​ (b)  

Choose from the below words
mystring.assign(str)
strcpy
mystring.compare(str);
strstr
toupper
isdigit