Strings vs CStrings Flashcards

1
Q

String Constructors

default (1) string();

copy (2) string (const string& str);

substring (3) string (const string& str, size_t pos, size_t len = npos);

from c-string (4) string (const char* s);

from buffer (5) string (const char* s, size_t n);

fill (6) string (size_t n, char c);

range (7) template string (InputIterator first, InputIterator last);

initializer list (8) string (initializer_list il);

move (9) string (string&& str) noexcept;

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

Function of:

  1. stof
  2. stold
  3. stoi
  4. strtof
A
  1. Convert string to float
  2. String to long double
  3. string to int
  4. ?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

include-ing the two

A

C-strings (#include <cstring>)</cstring>

C++ strings (#include <string> )</string>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Declaring a C-string variable
  2. Declaring a C++ string object
A
  1. char str[10];
  2. string str;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initializing a C-string variable

A

char str1[11] = “Call home!”;

char str2[] = “Send money!”;

char str3[] = {‘O’, ‘K’, ‘\0’};

Last line above has same effect as:

char str3[] = “OK”;

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

Initializing a C++ string object

A

string str1(“Call home!”);

string str2 = “Send money!”;

string str3(“OK”);

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

Assigning to a C-string variable

A

Can’t do it, i.e., can’t do this:

char str[10];

str = “Hello!”;

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

Assigning to a C++ string object

A

string str;

str = “Hello”;

str = otherString;

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

Concatenating two C-strings

A

strcat(str1, str2);

strcpy(str, strcat(str1, str2));

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

Concatenating two C++ string objects

A

str1 += str2;

str = str1 + str2;

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

Copying a C-string variable

A

char str[20];

strcpy(str, “Hello!”);

strcpy(str, otherString);

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

Copying a C++ string object

A

string str;

str = “Hello”;

str = otherString;

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

Accessing a single character (cstring)

A

str[index]

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

Accessing a single character(Strinng Obj)

A

str[index]

str.at(index)

str(index, count)

]

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

Comparing two C-strings vs Comparing two C++ string objects

A
17
Q

Finding the length of a C-string

A

strlen(str)

18
Q

Finding the length of a C++ string object

A

str.length()

19
Q

In what follows, keep in mind that cin ignores white space when reading a string, while cin.get(), cin.getline() and getline() do not. Remember too that cin.getline() andgetline() consume the delimiter while cin.get() does not. Finally, cin can be replaced with any open input stream, since file input with inFile, say, behaves in a manner completely analogous to the corresponding behavior of cin. Analogously, in the output examples given immediately above, cout could be replaced with any text output stream variable, say outFile. In all cases, numCh is the maximum number of characters that will be read.

A
20
Q
A