String and Vector Functions in C++
1. isalnum cctype or ctype.h library what does it have (13
isalpha but know the two big ones)
isblank
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
tolower
toupper
2. isprint(c) Returns true if c is a printable character. Print-
able characters include alphanumeric, punctu-
ation, and space characters.
3. iscntrl(c) Returns true if c is a control character. Control
characters are all characters that are not print-
able.
4. isxdigit(c) Returns true if c is a hexadecimal digit: 0-9, a-f,
A-F.
5. length(), [], push_back, substr what is included in <string>
6. pop_back(), push_back(), size() what is included in <vector>
7. char greeting2[] = "Hello"; // C-Style string greeting1 = "Hello"; // Regular String
String (an array of characters)
8. at()
, String and Vector Functions in C++
Returns the character at a specified index, with
bounds checking
9. back() Accesses the last character in the string
10. begin() Returns an iterator pointing to the first character
of the string
11. std::string myStr = "Hello"; explain .c_str() (give an example)
const char* cString = myStr.c_str();
myStr is a std::string
cString now points to a null-terminated
character array like "Hello\0"
12. C++: s.length() or s.size() Q: How do you find the length of a string?
C-style: strlen(s) (from <cstring>)
13. A: Q: How do you concatenate strings?
C++: s1 + s2, s += "text"
C-style: strcat(dest, src)
14. A: Q: How do you declare a C++ string vs. a C-style
std::string s = "Hello"; (needs #include string?
<string>)
char s[] = "Hello";
15. A: Q: How do you copy strings?
C++: std::string copy = original;
C-style: strcpy(dest, src)
16. A: Q: How do you compare strings for equality?
C++: s1 == s2
C-style: strcmp(s1, s2) == 0
, String and Vector Functions in C++
17. A: Q: How do you access individual characters in a
Both: s[i] string?
C++ also supports s.at(i) with bounds
checking
18. A: Q: How do you get a substring?
C++: s.substr(start, length)
C-style: No direct function; use strncpy()
manually
19. A: Q: How do you find a word inside a string (po-
C++: s.find("word") sition)?
std::string text = "Welcome to Earth"; give examples for both
size_t pos = text.find("Earth"); // returns
index of "Earth"
C-style: strstr(s, "word")
char text[] = "Welcome to Earth";
char* found = strstr(text, "Earth");
if (found != nullptr) {
int pos = found - text;
strstr() returns a pointer, so subtracting
the base address gives you the index.
20. A: Q: How do you replace part of a string? + exam-
C++: s.replace(pos, len, "new") ples
std::string s = "I love apples"; "I love apples" --> I hate apples
s.replace(2, 4, "hate");
C-style: Manually with strncpy()
1. isalnum cctype or ctype.h library what does it have (13
isalpha but know the two big ones)
isblank
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
tolower
toupper
2. isprint(c) Returns true if c is a printable character. Print-
able characters include alphanumeric, punctu-
ation, and space characters.
3. iscntrl(c) Returns true if c is a control character. Control
characters are all characters that are not print-
able.
4. isxdigit(c) Returns true if c is a hexadecimal digit: 0-9, a-f,
A-F.
5. length(), [], push_back, substr what is included in <string>
6. pop_back(), push_back(), size() what is included in <vector>
7. char greeting2[] = "Hello"; // C-Style string greeting1 = "Hello"; // Regular String
String (an array of characters)
8. at()
, String and Vector Functions in C++
Returns the character at a specified index, with
bounds checking
9. back() Accesses the last character in the string
10. begin() Returns an iterator pointing to the first character
of the string
11. std::string myStr = "Hello"; explain .c_str() (give an example)
const char* cString = myStr.c_str();
myStr is a std::string
cString now points to a null-terminated
character array like "Hello\0"
12. C++: s.length() or s.size() Q: How do you find the length of a string?
C-style: strlen(s) (from <cstring>)
13. A: Q: How do you concatenate strings?
C++: s1 + s2, s += "text"
C-style: strcat(dest, src)
14. A: Q: How do you declare a C++ string vs. a C-style
std::string s = "Hello"; (needs #include string?
<string>)
char s[] = "Hello";
15. A: Q: How do you copy strings?
C++: std::string copy = original;
C-style: strcpy(dest, src)
16. A: Q: How do you compare strings for equality?
C++: s1 == s2
C-style: strcmp(s1, s2) == 0
, String and Vector Functions in C++
17. A: Q: How do you access individual characters in a
Both: s[i] string?
C++ also supports s.at(i) with bounds
checking
18. A: Q: How do you get a substring?
C++: s.substr(start, length)
C-style: No direct function; use strncpy()
manually
19. A: Q: How do you find a word inside a string (po-
C++: s.find("word") sition)?
std::string text = "Welcome to Earth"; give examples for both
size_t pos = text.find("Earth"); // returns
index of "Earth"
C-style: strstr(s, "word")
char text[] = "Welcome to Earth";
char* found = strstr(text, "Earth");
if (found != nullptr) {
int pos = found - text;
strstr() returns a pointer, so subtracting
the base address gives you the index.
20. A: Q: How do you replace part of a string? + exam-
C++: s.replace(pos, len, "new") ples
std::string s = "I love apples"; "I love apples" --> I hate apples
s.replace(2, 4, "hate");
C-style: Manually with strncpy()