OOP2 Strings Flashcards

1
Q

What type of data type is the string type?

A

Programmer defined type, not part of base C++

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

What library do you need to access std::string data type?

A

include <string></string>

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

What terminators do you need for strings?

A

“ … “

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

What are strings called with no characters?

A

Null/empty strings

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

What does length function do for std::string?

A

Returns unsigned int of how many chars are in string

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

What does size function do?

A

Returns unsigned int value of amount of chars in the string, same as .length() but works also with vector, arrays etc

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

What type is the unsigned int returned by length, size, etc?

A

std::string::size_type

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

What does .find() do?

A

Find returns an int of type string::size_type corresponding to the index of the string where the first char of the passed string was matched

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

What argument does .find() take?

A

It takes a substring to be found in the operated string

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

Find is case sensitive, true or false?

A

True

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

What happens if .find() finds no match?

A

Returns an named const of type string::npos

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

What happens if there are multiple identical subtrings for .find() to find?

A

Considers only the first

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

What does .substr() do?

A

Returns the piece of the string that starts at the specified index and continues for the number of chars specified

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

What arguments does .substr() take?

A

Substring takes two integers, first is starting index, second is length of sub string

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

Where does substring start counting from for length of returned string?

A

1 counted from same index as first argument

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

What does .at() function do?

A

Returns char at index of passed argument

17
Q

What argument does .at() function take?

A

It takes an integer type and uses it as index value

18
Q

How is string type different from char arrays / c strings in terms of
1. program runtime integrity,
2. in terms of how they are terminated?

A
  1. Strings have bound checking at compile time, arrays do not
  2. Strings are terminated with the enclosing speech marks, char arrays / c strings are terminated with the null character
19
Q

What library is needed for toupper() and tolower()?

A

include <cctype></cctype>

20
Q

What arguments do toupper() and tolower() take?

A

A single char data type

21
Q

Other than .at(), how can you access chat at index?

A

std::string myString = “hello”;
ch = myString[4];
ch will contain ‘o’

22
Q

How to concentrate strings?

A

+ operator or .append()

23
Q

What does .compare() return?

A

0 if they are equal,
< 0 if the the passed string is shorter/first character doesn’t match
> 0 if the passed string is longer or first unmatching char is longer

24
Q

What does .insert() take?

A

2 arguments: 1. Index, 2. String

25
Q

How to obtain a cstring from a string?

A

myString.c_str();

26
Q

How to clear a string and leave it empty/null?

A

myString.erase();

27
Q

.replace takes?

A

Three arguments, first index where to start, second index where to end, third the string to replace with

28
Q

.begin() and .end()?

A

Returns the index of the beginning and past-the-end characters of strings

29
Q

.swap()?

A

Exchanges the content of two strings

30
Q

.pop_back()?

A

Deletes last char of string

31
Q

.push_back()?

A

Adds a new character at end of string, takes a single character as argument

32
Q

Difference between [] and .at()?

A

[] does not bound check but does not throw an error,
.at() bounds checks

33
Q

Erase takes two optional arguments. What is it?

A

Int for index from which to erase, and int for length for how long to erase