Strings
Strings
#include <string>
#include <regex>
int main() {
// ii. Concatenate your name with a greeting message and display the result
// iii. Use string slicing to extract a substring from your name (e.g., the first three characters)
// iv. Replace a specific character in your name with another character and display the modified
name
if (pos != std::string::npos) {
modifiedName[pos] = replacementChar;
std::string emailAddress;
std::regex emailPattern(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)");
if (std::regex_match(emailAddress, emailPattern)) {
} else {
return 0;
Control structures
Conditional statements allow you to execute different blocks of code based on certain
conditions.
In C++, the primary conditional statements are if, else if, and else.
int x = 10;
if (x > 0) {
} else if (x < 0) {
} else {
Loops:
Loops allow you to execute a block of code repeatedly as long as a condition is true.
The main loop constructs in C++ are for, while, and do-while loops.
int i = 0;
while (i < 5) {
++i;
int i = 0;
do {
++i;
continue statement: Skips the remaining code in the current iteration of a loop and
continues with the next iteration.
break statement: Exits the loop immediately, regardless of the loop condition.
if (i % 2 == 0) {
if (i == 7) {
}
#include <iostream>
int main() {
int score;
case 0:
case 1:
case 2:
case 3:
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
case 9:
case 10:
std::cout << "High Distinction" << std::endl;
break;
default:
break;
return 0;
Using if statement
#include <iostream>
int main() {
// Declare variables
int score;
} else {
return 0;
int score;
// Prompt user to enter exam score
case 10:
case 9:
case 8:
break;
case 7:
break;
case 6:
break;
case 5:
break;
default:
break;