C++ Deck 8 Flashcards

1
Q

What is a c-string?

A

A char array that ends with the null character ‘\0’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is null termination?

A

Since array size needs to be known at compile time, the null character \0 is inserted at the end of a c-string array to let the compiler know that the string has ended (even if all allocated spaces in the array are not currently used.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are three things that a c-string can do that a typical integer array cannot?

A
  1. Directly initialize with a quoted literal.
  2. Directly print with cout (no loop required)
  3. Directly read one “word” with cin (again, no loop required)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can a c-string store character literals in single quotes such as ‘a’, ‘\n’, and ‘$’?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the function:

int toupper(int c)

do?

A

Returns the uppercase version of c if it’s a lowercase letter, otherwise returns c as-is

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the function:

int tolower(int c)

do?

A

Returns the lowercase version of c if it’s an uppercase letter, otherwise returns c as-is.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the function:

int isdigit(int c)

do?

A

Determines whether the parameter is a digit (0-9) and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the function:

int isalpha(int c)

do?

A

Determines whether the character is a letter (a-z, A-Z) and returns true or false depending on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the function:

int isdigit(int c)

do?

A

Determines whether or not c is a letter OR a number and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the function:

int islower(int c)

do?

A

Determines whether or not the character is lowercase and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the function:

int isupper(int c)

do?

A

Determines whether or not c is uppercase and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the function:

int isxdigit(int c)

do?

A

Determines whether or not a c is a hex character (0-9, a-f) and returns true or false as a result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the function:

int isspace(int c)

do?

A

Determines whether or not c is a whitespace character and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the function:

int iscntrl(int c)

do?

A

Determines whether or not c is a control character (e.g. ‘\n’) and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the function:

int ispunct(int c)

do?

A

Determines whether or not c is a printable character (other than a space, letter, or digit) and returns true or false based on the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the function:

int isprint(int c)

do?

A

Determines whether or not c is a printable character (including space) and returns true or false based on the result.

17
Q

What does the function:

int isgraph(int c)

do?

A

Determines whether or not c is a printable character (excluding space) and returns true or false based on the result.

18
Q

What is the standard string library in C?

A

<cstring>
</cstring>

19
Q

What does the function:

int strlen(const char str[]);

do?

A

Returns the length of the target string.

cout &laquo_space;strlen(“Greetings, Earthling!”); // returns 12

char phrase[30] = “Hello, World”;
int length = strlen(phrase); // stores 12

20
Q

What does the function:

char* strcpy(char str1[], const char str2[]);

do?

A

Copies the contents of the second string (which is constant and unchanged) into the first string, which is non-constant.

strcpy(firstname, “Billy Joe Bob”); // copies name into firstname array

char buffer[80], lastname[30] = “Smith”;
strcpy(buffer, lastname); // copies “Smith” into buffer array

21
Q

What does the function:

char* strcat(char str1[], const char str2[]);

do?

A

Concatenates the second string onto the first.

char buffer[30] = “Batman”
strcat(buffer, “ is awesome.”); // buffer is now: “Batman is awesome.”

22
Q

What does the function:

int strcmp(const char str1[], const char str2[]);

do?

A

Returns a negative number if str1 comes before str2, a positive number if str2 comes before str1, and a 0 if they are equal.

Lexicographic order is determined by ASCII codes and not alphabetic order. Any uppercase letter has precedence over any lower case in ASCII. Z comes before a, for example.

23
Q

What do strncpy, strncat, and strncmp do?

A

The same things as strcpy, strcat, and strcmp, but they have a built-in memory safe parameter that allows you specify a stopping point other than the null character.

24
Q

What is the priority of lexicographic ordering?

A

numeral characters -> uppercase characters -> lowercase characters

25
Q
A