#include <iostream>
#include <string>
#include <regex>
int main() {
// i. Declare and initialize a string variable with your name
std::string name = "John";
// ii. Concatenate your name with a greeting message and display the result
std::string greeting = "Hello, " + name + "!";
std::cout << greeting << std::endl;
// iii. Use string slicing to extract a substring from your name (e.g., the first three characters)
std::string substring = name.substr(0, 3);
std::cout << "Substring of name: " << substring << std::endl;
// iv. Replace a specific character in your name with another character and display the modified
name
char originalChar = 'o';
char replacementChar = 'x';
std::string modifiedName = name;
size_t pos = modifiedName.find(originalChar);
if (pos != std::string::npos) {
modifiedName[pos] = replacementChar;
std::cout << "Modified name: " << modifiedName << std::endl;
// v. Use regular expressions to validate an email address entered by the user
std::string emailAddress;
std::cout << "Enter an email address: ";
std::cin >> emailAddress;
// Regular expression pattern to validate email address format
std::regex emailPattern(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)");
// Check if the entered email address matches the pattern
if (std::regex_match(emailAddress, emailPattern)) {
std::cout << "Valid email address." << std::endl;
} else {
std::cout << "Invalid email address." << std::endl;
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) {
std::cout << "x is positive." << std::endl;
} else if (x < 0) {
std::cout << "x is negative." << std::endl;
} else {
std::cout << "x is zero." << std::endl;
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.
Example of for loop:
Q1
Example of while loop:
int i = 0;
while (i < 5) {
std::cout << i << std::endl;
++i;
Example of do-while loop:
int i = 0;
do {
std::cout << i << std::endl;
++i;
} while (i < 5);
Jump Statements and Implementation of continue and break Statements in Loops:
Jump statements allow you to alter the flow of control in a program.
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.
for (int i = 0; i < 10; ++i) {
if (i % 2 == 0) {
continue; // Skip even numbers
std::cout << i << std::endl;
if (i == 7) {
break; // Exit loop when i equals 7
}
#include <iostream>
int main() {
int score;
// Prompt the user to enter the exam score
std::cout << "Enter the exam score: ";
std::cin >> score;
// Display corresponding message based on the score range using switch
switch (score / 10) {
case 0:
case 1:
case 2:
case 3:
case 4:
std::cout << "Fail" << std::endl;
break;
case 5:
std::cout << "Pass" << std::endl;
break;
case 6:
std::cout << "Credit" << std::endl;
break;
case 7:
std::cout << "Distinction" << std::endl;
break;
case 8:
case 9:
case 10:
std::cout << "High Distinction" << std::endl;
break;
default:
std::cout << "Invalid score" << std::endl;
break;
return 0;
Score below 50: Fail
Score 50-64: Pass
Score 65-74: Credit
Score 75-84: Distinction
Score 85-100: High Distinction
Using if statement
#include <iostream>
using namespace std;
int main() {
// Declare variables
int score;
// Prompt user to enter exam score
cout << "Enter your exam score: ";
cin >> score;
// Check score range and display corresponding message
if (score < 50) {
cout << "Fail" << endl;
} else if (score >= 50 && score <= 64) {
cout << "Pass" << endl;
} else if (score >= 65 && score <= 74) {
cout << "Credit" << endl;
} else if (score >= 75 && score <= 84) {
cout << "Distinction" << endl;
} else if (score >= 85 && score <= 100) {
cout << "High Distinction" << endl;
} else {
cout << "Invalid score entered" << endl;
return 0;
Using switch statement
int score;
// Prompt user to enter exam score
cout << "Enter the student's exam score: ";
cin >> score;
// Using switch to determine the message based on score range
switch (score / 10) {
case 10:
case 9:
case 8:
cout << "High Distinction";
break;
case 7:
cout << "Distinction";
break;
case 6:
cout << "Credit";
break;
case 5:
cout << "Pass";
break;
default:
cout << "Fail";
break;