C++ Deck 8 Flashcards
What is a c-string?
A char array that ends with the null character ‘\0’
What is null termination?
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.)
What are three things that a c-string can do that a typical integer array cannot?
- Directly initialize with a quoted literal.
- Directly print with cout (no loop required)
- Directly read one “word” with cin (again, no loop required)
Can a c-string store character literals in single quotes such as ‘a’, ‘\n’, and ‘$’?
Yes.
What does the function:
int toupper(int c)
do?
Returns the uppercase version of c if it’s a lowercase letter, otherwise returns c as-is
What does the function:
int tolower(int c)
do?
Returns the lowercase version of c if it’s an uppercase letter, otherwise returns c as-is.
What does the function:
int isdigit(int c)
do?
Determines whether the parameter is a digit (0-9) and returns true or false based on the result.
What does the function:
int isalpha(int c)
do?
Determines whether the character is a letter (a-z, A-Z) and returns true or false depending on the result.
What does the function:
int isdigit(int c)
do?
Determines whether or not c is a letter OR a number and returns true or false based on the result.
What does the function:
int islower(int c)
do?
Determines whether or not the character is lowercase and returns true or false based on the result.
What does the function:
int isupper(int c)
do?
Determines whether or not c is uppercase and returns true or false based on the result.
What does the function:
int isxdigit(int c)
do?
Determines whether or not a c is a hex character (0-9, a-f) and returns true or false as a result.
What does the function:
int isspace(int c)
do?
Determines whether or not c is a whitespace character and returns true or false based on the result.
What does the function:
int iscntrl(int c)
do?
Determines whether or not c is a control character (e.g. ‘\n’) and returns true or false based on the result.
What does the function:
int ispunct(int c)
do?
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.
What does the function:
int isprint(int c)
do?
Determines whether or not c is a printable character (including space) and returns true or false based on the result.
What does the function:
int isgraph(int c)
do?
Determines whether or not c is a printable character (excluding space) and returns true or false based on the result.
What is the standard string library in C?
<cstring>
</cstring>
What does the function:
int strlen(const char str[]);
do?
Returns the length of the target string.
cout «_space;strlen(“Greetings, Earthling!”); // returns 12
char phrase[30] = “Hello, World”;
int length = strlen(phrase); // stores 12
What does the function:
char* strcpy(char str1[], const char str2[]);
do?
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
What does the function:
char* strcat(char str1[], const char str2[]);
do?
Concatenates the second string onto the first.
char buffer[30] = “Batman”
strcat(buffer, “ is awesome.”); // buffer is now: “Batman is awesome.”
What does the function:
int strcmp(const char str1[], const char str2[]);
do?
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.
What do strncpy, strncat, and strncmp do?
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.
What is the priority of lexicographic ordering?
numeral characters -> uppercase characters -> lowercase characters