C++ PROGRAMMING COURSE
1 Day Streak 0 XP
DAY 01

Introduction to C++

Start your programming journey by understanding what C++ is, why it is used and how it differs from C.

START DAY 01
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!";
    return 0;
}

Course Introduction

01

Learn C++ from the foundation

This course is designed to take you from the fundamentals of C++ programming to advanced concepts. Each day focuses on a specific topic followed by practice and MCQ tests.

Day-wise Learning Concept Based MCQ Tests Practice
DAY 01 • LESSON 01

Introduction to C++

Understand the foundation of C++ programming.

01

What is C++?

C++ is a cross-platform language that can be used to create high-performance applications.

C++ was developed by Bjarne Stroustrup, as an extension to the C language.

02

Why Use C++?

  • C++ is one of the world's most popular programming languages.
  • C++ can be found in today's operating systems, Graphical User Interfaces and embedded systems.
  • C++ is an object-oriented programming language which gives a clear structure to programs.
  • C++ allows code to be reused, lowering development costs.
03

Difference Between C and C++

C++ was developed as an extension of C, and both languages have almost the same syntax.

C
C++
Procedural programming language
Supports object-oriented programming
Does not support classes and objects
Supports classes and objects
TOPIC 05

Basic Syntax of C++

Every C++ program follows a basic structure. Understanding this structure is important before writing more complex programs.

प्रत्येक C++ program एक basic structure को follow करता है। बड़े programs लिखने से पहले इस structure को समझना जरूरी है।

Basic C++ Program

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World";

    return 0;
}

Explanation

  • #include <iostream> — Includes the input/output library.
  • using namespace std; — Allows us to use standard C++ features without writing std:: every time.
  • int main() — The main function where program execution begins.
  • cout — Used to display output on the screen.
  • return 0; — Indicates that the program has completed successfully.
  • { } — Define the beginning and end of a block of code.
  • ; — Used to terminate most C++ statements.
  • #include <iostream> — Input/Output library को program में include करता है।
  • using namespace std; — Standard C++ features को बिना बार-बार std:: लिखे use करने देता है।
  • int main() — Program का execution यहीं से शुरू होता है।
  • cout — Screen पर output दिखाने के लिए use होता है।
  • return 0; — बताता है कि program successfully complete हुआ।
  • { } — Code block की शुरुआत और अंत को define करते हैं।
  • ; — अधिकांश C++ statements के अंत में लगाया जाता है।
TOPIC 06

Comments in C++

Comments are notes written inside the source code. They are ignored by the compiler and are mainly used to explain the code.

Comments source code के अंदर लिखे गए notes होते हैं। Compiler comments को ignore करता है। इनका उपयोग code को समझाने के लिए किया जाता है।

1. Single-Line Comment

A single-line comment starts with //.

Single-line comment // से शुरू होता है।

// This is a single-line comment

#include <iostream>
using namespace std;

int main()
{
    // Display message
    cout << "Hello World";

    return 0;
}

2. Multi-Line Comment

A multi-line comment starts with /* and ends with */.

Multi-line comment /* से शुरू होकर */ पर समाप्त होता है।

/*
   This is a
   multi-line comment
*/

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World";

    return 0;
}

Quick Revision

// → Single-line comment

/* ... */ → Multi-line comment

#include <iostream> → Input/Output library

main() → Program execution starts here

cout → Output

; → Statement terminator

IMPORTANT

The main difference between C and C++ is that C++ supports classes and objects, while C does not.

DAY 01

Lesson Summary

3 Topics
Beginner Level
100% Completed
DAY 02 • LESSON 01

C++ Data Types

Learn the basic data types used in C++ programs.

01

C++ Data Types

Data types specify the type of data that a variable can store.

In this lesson, we will learn four basic data types: int, char, string, and float.

02

int — Integer

int is used to store whole numbers.

Examples: 1, 2, 3, 10, 100

int a = 10;
int b = 20;
03

char — Character

char is used to store a single character.

It can store letters such as a to z and A to Z.

A character is written inside single quotes (' ').

char grade = 'A';
char letter = 'b';
04

string — Text

string is used to store a sequence of characters.

A string can contain a word, name, or sentence.

A string is written inside double quotes (" ").

string name = "Gautam";
string message = "Hello World";
05

float — Decimal Value

float is used to store decimal values.

Examples: 1.23, 4.24, 10.5

float a = 1.23;
float b = 4.24;
PROGRAM 01

Write a C++ Program to Add Two Numbers

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;

    int sum = a + b;

    cout << "Sum = " << sum;

    return 0;
}
Output: Sum = 30

Here, a + b adds the two numbers and stores the result in sum.

PROGRAM 02

Write a C++ Program to Subtract Two Numbers

#include <iostream>
using namespace std;

int main() {

    int a = 20;
    int b = 10;

    int difference = a - b;

    cout << "Difference = " << difference;

    return 0;
}
Output: Difference = 10
PROGRAM 03

Write a C++ Program to Multiply Two Numbers

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;

    int product = a * b;

    cout << "Product = " << product;

    return 0;
}
Output: Product = 200
PROGRAM 04

Write a C++ Program to Divide Two Numbers

#include <iostream>
using namespace std;

int main() {

    float a = 20;
    float b = 5;

    float quotient = a / b;

    cout << "Quotient = " << quotient;

    return 0;
}
Output: Quotient = 4
QUICK REVISION

Data Types at a Glance

Data Type Used For Example
int Whole numbers 10
char Single character 'A'
string Text / Sentence "Hello"
float Decimal values 1.23
OPERATORS USED

+ → Addition

- → Subtraction

* → Multiplication

/ → Division

DAY 02

C++ Data Types

01 Lesson
04 Data Types
04 Programs
Beginner Level
DAY 03 • LESSON 01

C++ Data Types

Learn char, string and float with simple C++ examples.

01

char — Character

char is used to store a single character.

It can store characters from a to z and A to Z.

Example

#include <iostream>
using namespace std;

int main() {

    char letter = 'A';

    cout << "Character = " << letter;

    return 0;
}
Output: Character = A
02

string — Text

string is used to store a sequence of characters.

Characters can be combined to make a word, name or sentence. This is called a string.

Example

#include <iostream>
using namespace std;

int main() {

    string message = "Hello World";

    cout << message;

    return 0;
}
Output: Hello World
03

float — Decimal Value

float is used to store decimal values.

Examples: 1.23, 4.24, 10.5

Complete Code Example

#include <iostream>
using namespace std;

int main() {

    float a = 20;
    float b = 5;

    float quotient = a / b;

    cout << "Quotient = " << quotient;

    return 0;
}
Output: Quotient = 4
DAY 03

C++ Data Types

01 Lesson
03 Topics
03 Examples
Beginner Level
DAY 04 • LESSON 01

Swap Two Numbers

Learn how to swap two numbers using a third variable.

01

Swap Two Numbers Using a Third Variable

Write a C++ program to swap two numbers using a third variable.

Suppose: a = 10 and b = 20.

STEP 01

Store the Value of a in c

a = 10 b = 20 c = 0

First, store the value of a in the third variable c.

c = a;
After Step 1: a = 10, b = 20, c = 10
STEP 02

Store the Value of b in a

Now, store the value of b in a.

a = b;
After Step 2: a = 20, b = 20, c = 10
STEP 03

Store the Value of c in b

Finally, store the original value of a, which is saved in c, into b.

b = c;
After Step 3: a = 20, b = 10, c = 10
FINAL RESULT

Before swapping: a = 10, b = 20

After swapping: a = 20, b = 10

The third variable c temporarily stores the original value of a.

COMPLETE CODE

C++ Program to Swap Two Numbers

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;
    int c = 0;

    c = a;
    a = b;
    b = c;

    cout << "a = " << a << endl;
    cout << "b = " << b;

    return 0;
}
Output:
a = 20
b = 10
DAY 04

Swap Two Numbers

01 Lesson
03 Steps
01 Program
Beginner Level
DAY 05 • LESSON 01

Variables, Data Types & Operators

Learn variables, data types, input/output and operators in C++.

01

Variables and Constants

A variable is used to store data that can be changed during the execution of a program.

A constant is a value that cannot be changed after it is defined.

02

Data Types

Data types define the type of data that a variable can store.

Examples: int, char, string, float

03

Keywords and Identifiers

Keywords are reserved words in C++ that have a special meaning.

Examples: int, float, return, if

Identifiers are names given to variables, functions and other program elements.

04

Input and Output — cin, cout

cin is used to take input from the user.

cout is used to display output on the screen.

06

Relational Operators

Relational operators are used to compare two values. The result of a relational operation is either true or false.

Operator Name Example Result
== Equal to 10 == 10 true
!= Not equal to 10 != 20 true
> Greater than 20 > 10 true
< Less than 10 < 20 true
>= Greater than or equal to 20 >= 20 true
<= Less than or equal to 10 <= 20 true

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 20;
    int b = 10;

    cout << (a == b) << endl;
    cout << (a != b) << endl;
    cout << (a > b) << endl;
    cout << (a < b) << endl;
    cout << (a >= b) << endl;
    cout << (a <= b) << endl;

    return 0;
}
Output:
0
1
1
0
1
0

Note: In C++, 1 = true and 0 = false.

07

Logical Operators

Logical operators are used to combine two or more conditions.

Operator Name Meaning Example
&& AND Both conditions must be true a > 10 && b < 20
|| OR At least one condition must be true a > 10 || b < 20
! NOT Reverses the result !(a > b)

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 20;
    int b = 10;

    // AND
    cout << (a > 10 && b < 20) << endl;

    // OR
    cout << (a > 30 || b < 20) << endl;

    // NOT
    cout << !(a < b) << endl;

    return 0;
}
Output:
1
1
1

Note: In C++, Logical Operators return 1 (true) or 0 (false).

07.1

Bitwise XOR Operator

The ^ operator is called Bitwise XOR. It compares the corresponding bits of two numbers.

Operator Name Rule Example
^ Bitwise XOR Different bits → 1, Same bits → 0 5 ^ 3

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 3;

    int result = a ^ b;

    cout << "XOR = " << result;

    return 0;
}
Output:
XOR = 6

Important: ^ is a Bitwise XOR operator, not a Logical Operator.

08

Assignment Operators

Assignment operators are used to assign or update values in variables.

Operator Name Example Meaning
= Assignment a = 10 a में 10 store
+= Add and Assign a += 5 a = a + 5
-= Subtract and Assign a -= 5 a = a - 5
*= Multiply and Assign a *= 5 a = a * 5
/= Divide and Assign a /= 5 a = a / 5
%= Modulus and Assign a %= 5 a = a % 5

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 20;

    a += 5;
    cout << "After += : " << a << endl;

    a -= 5;
    cout << "After -= : " << a << endl;

    a *= 2;
    cout << "After *= : " << a << endl;

    a /= 2;
    cout << "After /= : " << a << endl;

    a %= 6;
    cout << "After %= : " << a << endl;

    return 0;
}
Output:
After += : 25
After -= : 20
After *= : 40
After /= : 20
After %= : 2
09

Increment and Decrement

The ++ operator increases a value by 1. The -- operator decreases a value by 1.

Operator Name Operation Example
++ Increment Value + 1 a++
-- Decrement Value - 1 a--

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 10;

    cout << "Initial value = " << a << endl;

    a++;
    cout << "After Increment = " << a << endl;

    a--;
    cout << "After Decrement = " << a << endl;

    return 0;
}
Output:
Initial value = 10
After Increment = 11
After Decrement = 10
05

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division and remainder.

Operator Name Operation Example
+ Addition a + b 10 + 5 = 15
- Subtraction a - b 10 - 5 = 5
* Multiplication a * b 10 * 5 = 50
/ Division a / b 10 / 5 = 2
% Modulus a % b 10 % 3 = 1

Complete Example

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 3;

    cout << "Addition = " << a + b << endl;
    cout << "Subtraction = " << a - b << endl;
    cout << "Multiplication = " << a * b << endl;
    cout << "Division = " << a / b << endl;
    cout << "Remainder = " << a % b << endl;

    return 0;
}
Output:
Addition = 13
Subtraction = 7
Multiplication = 30
Division = 3
Remainder = 1

+ → Addition

- → Subtraction

* → Multiplication

/ → Division

% → Remainder / Modulus

PROGRAM 01

Write a C++ Program to Swap Two Numbers Without Using a Third Variable

In this program, two numbers are swapped without using a third variable.

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;

    a = a + b;
    b = a - b;
    a = a - b;

    cout << "After swapping:" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b;

    return 0;
}
Output: After swapping:
a = 20
b = 10
DAY 05

Variables, Data Types & Operators

09 Topics
01 Program
Beginner Level
DAY 06

Conditional Statements

Learn XOR swap and conditional statements in C++.

01

XOR Swap

XOR can be used to swap two integer values without using a third variable.

Complete Example — Swap Two Numbers Using XOR

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;

    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

    cout << "After swapping:" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b;

    return 0;
}
Output:
After swapping:
a = 20
b = 10

Note: XOR swap is generally demonstrated with integer values.

02

if Statement

The if statement executes a block of code when a given condition is true.

Complete Example

#include <iostream>
using namespace std;

int main() {

    int age = 20;

    if (age >= 18) {
        cout << "You are eligible.";
    }

    return 0;
}
Output:
You are eligible.
03

if-else Statement

The if-else statement executes one block when the condition is true and another block when it is false.

Complete Example

#include <iostream>
using namespace std;

int main() {

    int number = 10;

    if (number > 0) {
        cout << "Positive number";
    }
    else {
        cout << "Negative number";
    }

    return 0;
}
Output:
Positive number
04

Nested if

A nested if means using one if statement inside another if statement.

Complete Example

#include <iostream>
using namespace std;

int main() {

    int age = 20;
    int id = 1;

    if (age >= 18) {

        if (id == 1) {
            cout << "Entry allowed";
        }

    }

    return 0;
}
Output:
Entry allowed
05

else-if Ladder

An else-if ladder is used to check multiple conditions one by one.

Complete Example

#include <iostream>
using namespace std;

int main() {

    int marks = 75;

    if (marks >= 90) {
        cout << "Grade A+";
    }
    else if (marks >= 75) {
        cout << "Grade A";
    }
    else if (marks >= 60) {
        cout << "Grade B";
    }
    else if (marks >= 50) {
        cout << "Grade C";
    }
    else {
        cout << "Fail";
    }

    return 0;
}
Output:
Grade A
06

switch Statement

The switch statement is used to select one block of code from multiple choices.

Complete Example

#include <iostream>
using namespace std;

int main() {

    int day = 2;

    switch (day) {

        case 1:
            cout << "Monday";
            break;

        case 2:
            cout << "Tuesday";
            break;

        case 3:
            cout << "Wednesday";
            break;

        default:
            cout << "Invalid day";
    }

    return 0;
}
Output:
Tuesday

Note: The break statement stops the execution of the switch block.

DAY 07

Loops

Learn for, while and do-while loops in C++.

01

for Loop

The for loop is used to execute a block of code repeatedly for a specific number of times.

Part Purpose Example
Initialization Starting value int i = 1
Condition Checks the condition i <= 5
Update Changes the value i++

Complete Example

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {
        cout << i << endl;
    }

    return 0;
}
Output:
1
2
3
4
5
02

while Loop

The while loop repeatedly executes a block of code as long as the given condition is true.

Step Purpose
Initialization Set the starting value
Condition Check the condition
Update Change the value inside the loop

Complete Example

#include <iostream>
using namespace std;

int main() {

    int i = 1;

    while (i <= 5) {

        cout << i << endl;

        i++;
    }

    return 0;
}
Output:
1
2
3
4
5
03

do-while Loop

The do-while loop executes the code block at least once and then checks the condition.

Step Working
1 Execute the code
2 Check the condition
3 Repeat if condition is true

Complete Example

#include <iostream>
using namespace std;

int main() {

    int i = 1;

    do {

        cout << i << endl;

        i++;

    } while (i <= 5);

    return 0;
}
Output:
1
2
3
4
5

Important: A do-while loop executes at least once, even when the condition is false initially.

04

Loop Comparison

Loop Condition Check Minimum Execution Common Use
for Before execution 0 times Known number of iterations
while Before execution 0 times Condition-based repetition
do-while After execution At least 1 time Menu/input-based programs
DAY 08

Break, Continue & Lambda Expressions

Learn break, continue and lambda expressions in C++.

01

break Statement

The break statement is used to stop the loop immediately.

Keyword Purpose Works In
break Stops the loop for, while, do-while, switch

Example 1 — Stop Loop

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {
            break;
        }

        cout << i << endl;
    }

    return 0;
}
Output:
1
2
3
4

Example 2 — Break with while Loop

#include <iostream>
using namespace std;

int main() {

    int i = 1;

    while (i <= 10) {

        if (i == 4) {
            break;
        }

        cout << i << endl;
        i++;
    }

    return 0;
}
Output:
1
2
3

Example 3 — Break with Number

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            break;
        }

        cout << i << " ";
    }

    return 0;
}
Output:
1 2
02

continue Statement

The continue statement skips the current iteration and moves to the next iteration.

Keyword Purpose
continue Skips the current iteration

Example 1 — Skip Number 3

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}
Output:
1
2
4
5

Example 2 — Skip Even Number

#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i % 2 == 0) {
            continue;
        }

        cout << i << " ";
    }

    return 0;
}
Output:
1 3 5

Example 3 — Skip Number 2

#include <iostream>
using namespace std;

int main() {

    int i = 0;

    while (i < 5) {

        i++;

        if (i == 2) {
            continue;
        }

        cout << i << " ";
    }

    return 0;
}
Output:
1 3 4 5
03

Lambda Expressions

A lambda expression is a small anonymous function that can be created directly in C++ code.

Part Meaning
[] Capture list
() Parameters
{} Function body

Example 1 — Simple Lambda

#include <iostream>
using namespace std;

int main() {

    auto greet = []() {
        cout << "Hello C++";
    };

    greet();

    return 0;
}
Output:
Hello C++

Example 2 — Lambda with Parameters

#include <iostream>
using namespace std;

int main() {

    auto add = [](int a, int b) {
        return a + b;
    };

    cout << add(10, 20);

    return 0;
}
Output:
30

Example 3 — Lambda with Variable

#include <iostream>
using namespace std;

int main() {

    int number = 10;

    auto square = [number]() {
        return number * number;
    };

    cout << square();

    return 0;
}
Output:
100
04

Quick Revision

Topic Purpose
break Stops the loop immediately
continue Skips the current iteration
Lambda Creates a small anonymous function
DAY 8(1)

Characters & Strings

Learn char, character input/output, strings and basic string operations.

TOPIC 01

Introduction to Character (char)

The char data type is used to store a single character in C++.

Example

#include <iostream>
using namespace std;

int main()
{
    char letter = 'A';

    cout << "Character = " << letter;

    return 0;
}

Output

Character = A

Important Points

  • char stores a single character.
  • Character values are written inside single quotes (' ').
  • Examples: 'A', 'b', '7', '@'
  • A char variable can store only one character at a time.
TOPIC 02

Character Input and Output

We can use cin to take a character as input and cout to display it.

Example

#include <iostream>
using namespace std;

int main()
{
    char letter;

    cout << "Enter a character: ";
    cin >> letter;

    cout << "You entered: " << letter;

    return 0;
}

Example Input

A

Output

You entered: A

cin → Input

cout → Output

char → Single character

TOPIC 03

Introduction to String

A string is used to store a sequence of characters such as a name, word or sentence.

Example

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    cout << "Name = " << name;

    return 0;
}

Output

Name = Rahul

Important Points

  • string stores multiple characters.
  • Strings are written inside double quotes (" ").
  • Examples: "Hello", "Rahul", "Hello World"
  • The <string> header is used for the C++ string class.
TOPIC 04

String Input and Output

We can use cin to take a single word and getline() to take a complete line or sentence as input.

Using cin

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Hello " << name;

    return 0;
}

Note: cin stops reading when it encounters whitespace.

Using getline()

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string message;

    cout << "Enter a sentence: ";
    getline(cin, message);

    cout << "You entered: " << message;

    return 0;
}

Example Input

Hello World

Output

You entered: Hello World
TOPIC 05

Basic String Operations

C++ provides several basic operations for working with strings.

1. Find String Length

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    cout << name.length();

    return 0;
}
5

2. String Concatenation

The + operator can be used to join strings.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first = "Hello";
    string second = "World";

    string result = first + " " + second;

    cout << result;

    return 0;
}
Hello World

3. Compare Strings

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a = "Hello";
    string b = "Hello";

    if (a == b)
    {
        cout << "Strings are equal";
    }

    return 0;
}
Strings are equal

4. Access Individual Characters

String characters can be accessed using an index. The first index is 0.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    cout << name[0];

    return 0;
}
R

5. Change a Character

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    name[0] = 'S';

    cout << name;

    return 0;
}
Sahul

Quick Revision

char → Stores one character

string → Stores multiple characters

cin → Takes input

cout → Displays output

getline() → Reads a complete line

length() → Finds string length

+ → Joins strings

== → Compares strings

string[0] → Accesses first character

DAY 8(2)

Character Functions & String Operations

Learn character functions, string traversal, length, comparison and manipulation.

TOPIC 01

Character Functions

C++ provides useful character functions such as isalpha(), isdigit(), toupper() and tolower().

1. isalpha()

Checks whether a character is an alphabet letter. It returns a non-zero value if the character is alphabetic.

#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';

    if (isalpha(ch))
    {
        cout << "It is an alphabet";
    }

    return 0;
}
It is an alphabet

2. isdigit()

Checks whether a character is a digit from 0 to 9.

#include <iostream>

using namespace std;

int main()
{
    char ch = '7';

    if (isdigit(ch))
    {
        cout << "It is a digit";
    }

    return 0;
}
It is a digit

3. toupper()

Converts a lowercase alphabet character into uppercase.

#include <iostream>

using namespace std;

int main()
{
    char ch = 'a';

    cout << char(toupper(ch));

    return 0;
}
A

4. tolower()

Converts an uppercase alphabet character into lowercase.

#include <iostream>

using namespace std;

int main()
{
    char ch = 'Z';

    cout << char(tolower(ch));

    return 0;
}
z

isalpha() → Checks alphabet

isdigit() → Checks digit

toupper() → Converts to uppercase

tolower() → Converts to lowercase

TOPIC 02

String Traversal

String traversal means accessing each character of a string one by one using an index or loop.

Using Index

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    cout << name[0] << endl;
    cout << name[1] << endl;
    cout << name[2] << endl;

    return 0;
}
R
a
h

Using for Loop

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    for (int i = 0; i < name.length(); i++)
    {
        cout << name[i] << endl;
    }

    return 0;
}
R
a
h
u
l
TOPIC 03

String Length

String length means the total number of characters present in a string.

Using length()

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name = "Rahul";

    cout << "Length = " << name.length();

    return 0;
}
Length = 5

Using size()

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text = "Hello";

    cout << "Length = " << text.size();

    return 0;
}
Length = 5

length() → Returns number of characters

size() → Also returns number of characters

TOPIC 04

String Comparison

Two strings can be compared using comparison operators such as == and !=.

Compare Two Strings

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first = "Hello";
    string second = "Hello";

    if (first == second)
    {
        cout << "Strings are equal";
    }

    return 0;
}
Strings are equal

Not Equal

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first = "Hello";
    string second = "World";

    if (first != second)
    {
        cout << "Strings are different";
    }

    return 0;
}
Strings are different
TOPIC 05

String Manipulation

String manipulation means performing operations such as joining, copying and searching strings.

1. String Concatenation

Concatenation means joining two or more strings.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first = "Hello";
    string second = "World";

    string result = first + " " + second;

    cout << result;

    return 0;
}
Hello World

2. String Copying

A string can be copied into another string using the assignment operator =.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first = "Hello";
    string second;

    second = first;

    cout << second;

    return 0;
}
Hello

3. String Searching

The find() function can be used to search for a character or substring.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text = "Hello World";

    size_t position = text.find("World");

    if (position != string::npos)
    {
        cout << "Found at position " << position;
    }

    return 0;
}
Found at position 6

Quick Revision

isalpha() → Checks alphabet

isdigit() → Checks digit

toupper() → Converts to uppercase

tolower() → Converts to lowercase

string[index] → Access character

length() → Finds string length

size() → Finds string length

== → Compares strings

+ → Concatenates strings

= → Copies a string

find() → Searches character/substring

DAY 8(03)

Character & String

Learn character variables, strings, vowels, string length and palindrome programs.

TOPIC 01

What is a Character (char)?

A char is a data type used to store a single character in C++. A character is written inside single quotes.

Declaration

char letter;

Initialization

char letter = 'A';

Complete Example

#include <iostream>
using namespace std;

int main()
{
    char letter = 'A';

    cout << "Character = " << letter;

    return 0;
}
Character = A

char → Stores one character

'A' → Character constant

char letter = 'A'; → Declaration + Initialization

TOPIC 02

What is a String?

A string is a sequence of characters used to store words, names or complete sentences.

Declaration

string name;

Initialization

string name = "Rahul";

String Input using cin

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Name = " << name;

    return 0;
}

Note: cin reads only one word. To read a complete sentence containing spaces, use getline().

String Input using getline()

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string message;

    cout << "Enter a sentence: ";
    getline(cin, message);

    cout << "You entered: " << message;

    return 0;
}

string name; → Declaration

string name = "Rahul"; → Initialization

cin >> name; → Single word input

getline(cin, name); → Complete line input

TOPIC 03

Count Vowels and Consonants in a String

In this program, we traverse the string character by character and count vowels and consonants separately.

Program

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string text;

    int vowels = 0;
    int consonants = 0;

    cout << "Enter a string: ";
    getline(cin, text);

    for (int i = 0; i < text.length(); i++)
    {
        char ch = tolower(text[i]);

        if (isalpha(ch))
        {
            if (ch == 'a' || ch == 'e' ||
                ch == 'i' || ch == 'o' ||
                ch == 'u')
            {
                vowels++;
            }
            else
            {
                consonants++;
            }
        }
    }

    cout << "Vowels = " << vowels << endl;
    cout << "Consonants = " << consonants;

    return 0;
}

Example Input

Hello World

Output

Vowels = 3
Consonants = 7

Logic

Step 1: Take a string as input.

Step 2: Traverse every character using a loop.

Step 3: Convert the character to lowercase.

Step 4: Check whether it is a vowel.

Step 5: Otherwise, if it is an alphabet, count it as a consonant.

TOPIC 04

Find String Length Without Built-in Length Function

We can find the length of a string manually by traversing every character and increasing a counter.

Program

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text;
    int length = 0;

    cout << "Enter a string: ";
    getline(cin, text);

    for (char ch : text)
    {
        length++;
    }

    cout << "Length = " << length;

    return 0;
}

Example Input

Hello World

Output

Length = 11

length = 0 → Counter starts from 0

for (char ch : text) → Traverses every character

length++ → Increases counter for every character

TOPIC 05

Check Whether a String is Palindrome

A palindrome is a word or string that reads the same forward and backward.

Examples

MADAM → Palindrome

LEVEL → Palindrome

RADAR → Palindrome

HELLO → Not Palindrome

Program

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string text;

    cout << "Enter a string: ";
    cin >> text;

    bool palindrome = true;

    int start = 0;
    int end = text.length() - 1;

    while (start < end)
    {
        if (text[start] != text[end])
        {
            palindrome = false;
            break;
        }

        start++;
        end--;
    }

    if (palindrome)
    {
        cout << "Palindrome";
    }
    else
    {
        cout << "Not Palindrome";
    }

    return 0;
}

Example 1

Input: MADAM
Output: Palindrome

Example 2

Input: HELLO
Output: Not Palindrome

Logic

Step 1: Take a string as input.

Step 2: Set start = 0.

Step 3: Set end = length - 1.

Step 4: Compare characters from both ends.

Step 5: If any characters are different, it is not a palindrome.

Step 6: Move start forward and end backward.

QUICK REVISION

Day 13 Quick Revision

char → Stores a single character

string → Stores a sequence of characters

cin → Takes single-word input

getline() → Takes complete line input

Vowels → A, E, I, O, U

Consonant → Alphabet other than vowels

String traversal → Accessing characters one by one

Palindrome → Same forward and backward

start = 0 → First character index

end = length - 1 → Last character index

DAY 09

Functions

Learn functions, declaration, definition, parameters and return values in C++.

01

Functions

A function is a block of code that performs a specific task. Functions help us divide a large program into smaller and reusable parts.

A function runs when it is called.

Example: Simple Function

#include <iostream>
using namespace std;

void greet() {
    cout << "Hello C++";
}

int main() {

    greet();

    return 0;
}
Output
Hello C++
02

Function Declaration and Definition

Function declaration tells the compiler about the function before it is used. Function definition contains the actual code of the function.

A function can be declared before the main() function and defined later.

Example: Declaration and Definition

#include <iostream>
using namespace std;

// Function Declaration
void greet();

// Function Definition
void greet() {
    cout << "Welcome to C++";
}

int main() {

    greet();

    return 0;
}
Output
Welcome to C++
03

Function Parameters

Parameters are variables used to pass data to a function. They allow a function to work with different values.

Example: Function with Parameters

#include <iostream>
using namespace std;

void add(int a, int b) {

    cout << "Sum = " << a + b;
}

int main() {

    add(10, 20);

    return 0;
}
Output
Sum = 30
Note: The values 10 and 20 are passed as arguments to the function.
04

Return Value

A function can return a value to the place where it was called. The return statement is used to send a value back from a function.

Example: Function with Return Value

#include <iostream>
using namespace std;

int add(int a, int b) {

    return a + b;
}

int main() {

    int result = add(10, 20);

    cout << "Sum = " << result;

    return 0;
}
Output
Sum = 30
05

Quick Revision

Topic Meaning
Function Block of code for a specific task
Declaration Tells compiler about the function
Definition Contains the actual function code
Parameter Used to pass data to a function
return Sends a value back from the function
DAY 10

Functions, Arrays & Strings

Learn call by value, call by reference, recursion, arrays and strings in C++.

01

Call by Value

In Call by Value, a copy of the actual value is passed to the function. Changes made inside the function do not affect the original variable.

Example: Call by Value

#include <iostream>
using namespace std;

void change(int x) {
    x = 20;
}

int main() {

    int a = 10;

    change(a);

    cout << "a = " << a;

    return 0;
}
Output
a = 10
02

Call by Reference

In Call by Reference, a reference to the original variable is passed to the function. Changes made inside the function affect the original variable.

Example: Call by Reference

#include <iostream>
using namespace std;

void change(int &x) {
    x = 20;
}

int main() {

    int a = 10;

    change(a);

    cout << "a = " << a;

    return 0;
}
Output
a = 20
03

Recursion

Recursion is a process in which a function calls itself. A base condition is required to stop the recursive calls.

Example: Recursion

#include <iostream>
using namespace std;

void count(int n) {

    if (n == 0)
        return;

    cout << n << endl;

    count(n - 1);
}

int main() {

    count(5);

    return 0;
}
Output
5
4
3
2
1
04

One-Dimensional Arrays

A one-dimensional array stores multiple values of the same data type in a single sequence.

Example: One-Dimensional Array

#include <iostream>
using namespace std;

int main() {

    int numbers[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {
        cout << numbers[i] << endl;
    }

    return 0;
}
Output
10
20
30
40
50
05

Two-Dimensional Arrays

A two-dimensional array stores data in rows and columns. It is commonly used to represent tables and matrices.

Example: Two-Dimensional Array

#include <iostream>
using namespace std;

int main() {

    int numbers[2][2] = {
        {10, 20},
        {30, 40}
    };

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < 2; j++) {
            cout << numbers[i][j] << " ";
        }

        cout << endl;
    }

    return 0;
}
Output
10 20
30 40
06

Strings

A string is used to store a sequence of characters or text. In C++, the string data type is commonly used for text.

String Example 1: Store a Name

#include <iostream>
using namespace std;

int main() {

    string name = "Rahul";

    cout << "Name = " << name;

    return 0;
}
Output
Name = Rahul

String Example 2: Store a Sentence

#include <iostream>
using namespace std;

int main() {

    string message = "I am learning C++";

    cout << message;

    return 0;
}
Output
I am learning C++

String Example 3: Take String Input

#include <iostream>
using namespace std;

int main() {

    string name;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Hello " << name;

    return 0;
}
Output
Enter your name: Amit
Hello Amit

String Example 4: Find String Length

#include <iostream>
using namespace std;

int main() {

    string text = "Hello";

    cout << "Length = " << text.length();

    return 0;
}
Output
Length = 5

String Example 5: Join Two Strings

#include <iostream>
using namespace std;

int main() {

    string firstName = "Rahul";
    string lastName = "Sharma";

    string fullName = firstName + " " + lastName;

    cout << "Full Name = " << fullName;

    return 0;
}
Output
Full Name = Rahul Sharma
07

Quick Revision

Topic Key Point
Call by Value Copy of value is passed
Call by Reference Reference of original variable is passed
Recursion Function calls itself
1D Array Stores data in a single sequence
2D Array Stores data in rows and columns
String Stores characters or text

MCQ Test Series

Test your understanding after completing the lessons.

TEST 01

C++ Introduction Test

Basic questions based on Day 01.

5 Questions
15 Minutes
100 Marks
TEST 02

C++ Data Types Test

Basic questions based on Day 02 — Data Types and Arithmetic Operations.

15 Questions
15 Minutes
75 Marks
TEST 03

C++ Variables & Data Types Test

Basic questions based on Day 03 — char, string and float.

15 Questions
15 Minutes
75 Marks
TEST 04

C++ Operators & Swapping Test

Basic questions based on Day 04 — Operators and swapping two numbers.

15 Questions
15 Minutes
75 Marks
TEST 05

C++ Variables, Operators & Input/Output Test

Questions based on Day 05 — Variables, Constants, Operators, cin and cout.

15 Questions
15 Minutes
75 Marks
TEST 06

C++ Conditional Statements Test

Questions based on Day 06 — XOR Swap, if, if-else, Nested if, else-if Ladder and switch.

15 Questions
15 Minutes
75 Marks
TEST 07

C++ Loops Test

Questions based on Day 07 — for, while and do-while loops.

15 Questions
15 Minutes
75 Marks
TEST 08

C++ Break, Continue & Lambda Test

Questions based on Day 08 — break, continue and lambda expressions.

15 Questions
15 Minutes
75 Marks
TEST 09

C++ Functions Test

Questions based on Day 09 — functions, parameters and return values.

15 Questions
15 Minutes
75 Marks
TEST 10

C++ Functions, Arrays & Strings Test

Questions based on Day 10 — Call by Value, Call by Reference, Recursion, Arrays and Strings.

15 Questions
15 Minutes
75 Marks

Practice Area

Practice questions coming soon.

After completing each day, practice questions will be added here.