0% found this document useful (0 votes)
26 views31 pages

5th_Chap(Arrays & Strings)

Uploaded by

leeminhoking1312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views31 pages

5th_Chap(Arrays & Strings)

Uploaded by

leeminhoking1312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

GHS CHITTA BATTA

COMPUTER SCIENCE_2
nd
Year (Notes)

Introduction to Arrays
An array is a collection of items stored at contiguous memory locations. It allows you to store multiple
values of the same type in a single variable. Each value in an array is identified by an index or key.
Real-life Example: Think of an array as a row of mailboxes where each mailbox (index) holds a
specific letter (value). If you have 5 mailboxes, you can store 5 letters, and each letter is accessed by its
position in the row.
Concepts of Arrays
1. Indexing: Arrays are indexed from 0. The first element is at index 0, the second element is at
index 1, and so on.
2. Fixed Size: The size of an array is defined at the time of its creation and cannot be changed
dynamically.
3. Homogeneous Elements: All elements in an array are of the same data type (e.g., all integers, all
characters).
Representation of Arrays
1. Declaration and Initialization
 Declaration: Defines the type and size of the array.
 Initialization: Sets initial values for the array elements.
Syntax:
type arrayName[size];
Example:
int numbers[5]; // Declaration of an array of 5 integers
Initialization:
int numbers[5] = {1, 2, 3, 4, 5}; // Initialization with values
Diagram of Array Representation
Array Representation Diagram
Here's a simple diagram to represent an array in memory:
Memory Address | Value
------------------------
1000 | 1
1004 | 2
1008 | 3
1012 | 4
1016 | 5
Explanation:
 Memory Address: Arrays are stored in contiguous memory locations.
 Value: Each element is stored at a specific address and can be accessed using its index.
Accessing Array Elements
To access or modify an element in the array, you use its index.
Syntax:
arrayName[index] = value; // Set value at index
value = arrayName[index]; // Get value from index
Example:
numbers[2] = 10; // Sets the value at index 2 to 10

GHSs(Chap-5 Array & Strings) Page 1


cout << numbers[2]; // Prints the value at index 2, which is 10
Example Programs with Arrays
Example 1: Print Elements of an Array
Scenario: Print all elements in an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

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


cout << numbers[i] << " "; // Print each element
}

return 0;
}
Explanation: This program uses a for loop to iterate through the array and print each element.
Example 2: Sum of Array Elements
Scenario: Calculate the sum of elements in an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int sum = 0;

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


sum += numbers[i]; // Add each element to sum
}

cout << "Sum of elements: " << sum << endl;

return 0;
}
Explanation: This program calculates the sum of all elements in the array and prints the result.
Example 3: Find the Largest Element
Scenario: Find the largest element in an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int max = numbers[0];

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


if (numbers[i] > max) {
max = numbers[i]; // Update max if a larger element is found
}
}

cout << "Largest element: " << max << endl;

GHSs(Chap-5 Array & Strings) Page 2


return 0;
}
Explanation: This program iterates through the array to find and print the largest element.
Example 4: Reverse an Array
Scenario: Reverse the elements of an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

cout << "Original array: ";


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

// Reverse the array


for (int i = 0; i < 5 / 2; i++) {
int temp = numbers[i];
numbers[i] = numbers[5 - i - 1];
numbers[5 - i - 1] = temp;
}

cout << "Reversed array: ";


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

return 0;
}
Terminology Used in Arrays
Let's break down some key terms related to arrays in simple terms and provide real-life examples for
better understanding.
1. Element
 Definition: An individual item stored in an array.
 Example: In a shopping list array with items like "milk," "bread," and "eggs," each item is an
element.
2. Index
 Definition: The position of an element in the array, usually starting from 0.
 Example: If you have an array of books, the index of the first book might be 0, the second book
1, and so on.
3. Size
 Definition: The total number of elements the array can hold.
 Example: If your array is like a shelf with 10 compartments, the size is 10.
4. Subscript
 Definition: Another term for index; the number used to access a specific element.
 Example: On a numbered calendar, the subscript for the date "15" is 15.
5. Array Name

GHSs(Chap-5 Array & Strings) Page 3


 Definition: The identifier used to refer to the entire array.
 Example: In a list of students, if you name your array students, students is the array name.
Real-Life Examples
1. Grocery List
 Array: A list of groceries you need to buy.
 Elements: "Milk," "Bread," "Eggs," "Butter."
 Index: 0 for "Milk," 1 for "Bread," 2 for "Eggs," and 3 for "Butter."
2. Classroom Seating
 Array: Seats in a classroom.
 Elements: Each seat number (1, 2, 3, ...).
 Index: The position of each student in the classroom, starting from 0.
3. Monthly Expenses
 Array: A list of expenses for each month.
 Elements: January's expenses, February's expenses, etc.
 Index: 0 for January, 1 for February, etc.
4. Daily Temperatures
 Array: Temperatures recorded for each day of the week.
 Elements: Temperature values for Monday through Sunday.
 Index: 0 for Monday, 1 for Tuesday, and so on.
Advantages and Disadvantages of Arrays in C++
Advantages
1. Efficient Access
o Explanation: Arrays allow quick access to elements using indices.
o Example: Accessing the third book in a library's catalog array is instantaneous.
2. Contiguous Memory Allocation
o Explanation: Elements are stored in contiguous memory locations, which improves
access speed.
o Example: Think of an array as a row of mailboxes placed next to each other, making it
easy to access any mailbox quickly.
3. Simple Syntax
o Explanation: Arrays have straightforward syntax for accessing and manipulating data.
o Example: Adding a new item to your list of groceries involves simply specifying the
index and value.
4. Static Size
o Explanation: The size of an array is fixed, which can help manage memory usage.
o Example: A fixed-size array is like a bookshelf with a set number of shelves.
Disadvantages
1. Fixed Size
o Explanation: Once an array's size is defined, it cannot be changed.
o Example: If you have a shelf with 10 compartments but later need more space, you're
stuck with the 10 compartments.
2. Memory Wastage
o Explanation: If the array is larger than needed, the extra memory is wasted.
o Example: If you only use 3 out of 10 shelves in a bookshelf, the remaining 7 are wasted.
3. Difficult to Resize

GHSs(Chap-5 Array & Strings) Page 4


oExplanation: Arrays cannot be resized dynamically; you need to create a new array if
you need more space.
o Example: If you have a growing list of students and need more space, you'll have to
create a new, larger seating arrangement.
4. Limited Flexibility
o Explanation: Arrays do not offer built-in functions for operations like insertion or
deletion of elements.
o Example: Inserting a new item in a shopping list requires manually shifting items, unlike
more dynamic data structures.

Definition and Initialization of an Array


Array: An array is a collection of items stored at contiguous memory locations. Each item in an array is
of the same data type and can be accessed using an index.
Definition of a One-Dimensional Array
A one-dimensional array is like a single row of boxes where each box can hold a value. This is the
simplest form of an array.
Example: Think of a one-dimensional array as a single row of mailboxes, where each mailbox can hold
one letter.
Initialization of a One-Dimensional Array
Initialization is the process of assigning values to the elements of the array at the time of declaration.
Syntax:
Declaration:
type arrayName[size];
Initialization:
type arrayName[size] = {value1, value2, value3, ..., valueN};
Examples with Diagrams
1. Monthly Expenses Array
Scenario: You want to keep track of your expenses for each month.
Array:
 Declaration: float expenses[12]; (for 12 months)
 Initialization: float expenses[12] = {1200.50, 1300.75, 1100.60, 1400.20,
1350.00, 1250.00, 1500.30, 1600.00, 1700.10, 1450.75, 1550.40, 1650.90};
Diagram:
Memory Address | Value
------------------------
1000 | 1200.50
1004 | 1300.75
1008 | 1100.60
1012 | 1400.20
1016 | 1350.00
1020 | 1250.00
1024 | 1500.30
1028 | 1600.00
1032 | 1700.10
1036 | 1450.75
1040 | 1550.40
1044 | 1650.90
2. Daily Temperatures
Scenario: Store temperatures recorded each day for a week.

GHSs(Chap-5 Array & Strings) Page 5


Array:
 Declaration: float temperatures[7]; (for 7 days)
 Initialization: float temperatures[7] = {22.5, 23.0, 21.5, 24.0, 25.5, 26.0,
27.5};
Diagram:
Memory Address | Value
------------------------
1000 | 22.5
1004 | 23.0
1008 | 21.5
1012 | 24.0
1016 | 25.5
1020 | 26.0
1024 | 27.5
3. Student Scores
Scenario: Keep track of scores for students in a class.
Array:
 Declaration: int scores[5]; (for 5 students)
 Initialization: int scores[5] = {85, 90, 78, 88, 92};
Diagram:
Memory Address | Value
------------------------
1000 | 85
1004 | 90
1008 | 78
1012 | 88
1016 | 92
4. Weekly Hours Worked
Scenario: Track the number of hours worked each day of the week.
Array:
 Declaration: int hoursWorked[7]; (for 7 days)
 Initialization: int hoursWorked[7] = {8, 7, 9, 8, 6, 7, 5};
Diagram:
Memory Address | Value
------------------------
1000 | 8
1004 | 7
1008 | 9
1012 | 8
1016 | 6
1020 | 7
1024 | 5
Code Examples
Example 1: Print Array Elements
Scenario: Print all elements in an integer array.
C++ Code:
#include <iostream>
using namespace std;

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

GHSs(Chap-5 Array & Strings) Page 6


for (int i = 0; i < 5; i++) {
cout << numbers[i] << " "; // Print each element
}

return 0;
}
Example 2: Sum of Array Elements
Scenario: Calculate the sum of elements in an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[4] = {5, 10, 15, 20};
int sum = 0;

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


sum += numbers[i]; // Add each element to sum
}

cout << "Sum: " << sum << endl;

return 0;
}
Example 3: Find the Largest Element
Scenario: Find the largest number in an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[6] = {3, 8, 2, 9, 5, 7};
int max = numbers[0];

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


if (numbers[i] > max) {
max = numbers[i]; // Update max if current element is greater
}
}

cout << "Largest element: " << max << endl;

return 0;
}
Example 4: Reverse Array Elements
Scenario: Reverse the elements of an integer array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

// Print original array


cout << "Original array: ";

GHSs(Chap-5 Array & Strings) Page 7


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

// Reverse the array


for (int i = 0; i < 5 / 2; i++) {
int temp = numbers[i];
numbers[i] = numbers[5 - i - 1];
numbers[5 - i - 1] = temp;
}

// Print reversed array


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

return 0;
}

Accessing and Writing at an Index in Arrays


Accessing an Array Element
Accessing an Element: In an array, each element is identified by an index. To access or read the value
of an element, you use its index.
Example: Imagine an array as a row of mailboxes. If you want to read the letter in the 3rd mailbox, you
look at mailbox number 2 (since indexing starts from 0).
Diagram:
Array: [5, 10, 15, 20, 25]
Index: 0 1 2 3 4
 To access the value at index 2: Array[2] returns 15.
Writing at an Index
Writing at an Index: To modify or write a new value at a specific index, you directly assign a new
value to that index.
Example: If you want to replace the letter in the 2nd mailbox with a new letter, you open mailbox
number 1 and put the new letter inside.
Diagram:
Original Array: [5, 10, 15, 20, 25]
Index: 0 1 2 3 4
 Writing 30 at index 2:
Modified Array: [5, 10, 30, 20, 25]
Code Examples
Example 1: Access and Print Array Element
Scenario: Access and print the element at a specific index.
C++ Code:
#include <iostream>
using namespace std;

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

GHSs(Chap-5 Array & Strings) Page 8


int index = 2; // Accessing the element at index 2
cout << "Element at index " << index << ": " << numbers[index] << endl;

return 0;
}
Explanation: This code accesses the element at index 2 of the array and prints it.
Example 2: Modify Array Element
Scenario: Change the value at a specific index and print the modified array.
C++ Code:
#include <iostream>
using namespace std;

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

int index = 3; // Index to be modified


numbers[index] = 100; // Writing a new value at index 3

// Print the modified array


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

return 0;
}
Explanation: This code changes the value at index 3 to 100 and prints the entire array to show the
modification.
Example 3: Update Array Element Based on User Input
Scenario: Allow the user to input an index and a new value, then update the array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {5, 10, 15, 20, 25};
int index, newValue;

cout << "Enter index to update (0-4): ";


cin >> index;
cout << "Enter new value: ";
cin >> newValue;

if (index >= 0 && index < 5) {


numbers[index] = newValue; // Writing new value at the specified index

// Print the updated array


cout << "Updated array: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
} else {
cout << "Invalid index!" << endl;

GHSs(Chap-5 Array & Strings) Page 9


}

return 0;
}
Explanation: This code takes user input for the index and new value, updates the array at the given
index, and prints the updated array.
Example 4: Array with Multiple Indices Access and Modification
Scenario: Access and modify elements at multiple indices in the array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int numbers[6] = {2, 4, 6, 8, 10, 12};

// Access and print elements at indices 1, 3, and 5


cout << "Elements at indices 1, 3, 5: ";
for (int i = 1; i <= 5; i += 2) {
cout << numbers[i] << " ";
}
cout << endl;

// Modify elements at indices 0, 2, and 4


numbers[0] = 20;
numbers[2] = 30;
numbers[4] = 40;

// Print the modified array


cout << "Modified array: ";
for (int i = 0; i < 6; i++) {
cout << numbers[i] << " ";
}
cout << endl;

return 0;
}
Explanation: This code accesses and prints elements at specific indices, modifies elements at other
indices, and prints the entire array to show the changes.

Traversing an Array Using Different Loop Structures


Traversing an array means visiting each element in the array to perform some operation, such as printing
or modifying values. We can use various loop structures to traverse an array: for, while, and do-while
loops.
Examples with Diagrams
1. Daily Expenses Tracking
Scenario: You have a list of daily expenses and want to calculate the total amount spent each day for a
week.
Array:
 Declaration: float expenses[7] = {45.50, 60.25, 55.75, 40.00, 75.10, 50.60,
30.00};
Diagram:
Array: [45.50, 60.25, 55.75, 40.00, 75.10, 50.60, 30.00]

GHSs(Chap-5 Array & Strings) Page 10


Index: 0 1 2 3 4 5 6
Traversing Using Different Loops
1. For Loop
Scenario: Print all the expenses for each day.
C++ Code:
#include <iostream>
using namespace std;

int main() {
float expenses[7] = {45.50, 60.25, 55.75, 40.00, 75.10, 50.60, 30.00};

cout << "Daily Expenses: ";


for (int i = 0; i < 7; i++) {
cout << expenses[i] << " ";
}
cout << endl;

return 0;
}
Explanation: The for loop iterates from index 0 to 6, accessing and printing each element in the
expenses array.
2. While Loop
Scenario: Calculate and print the total amount spent for the week.
C++ Code:
#include <iostream>
using namespace std;

int main() {
float expenses[7] = {45.50, 60.25, 55.75, 40.00, 75.10, 50.60, 30.00};
int i = 0;
float total = 0;

while (i < 7) {
total += expenses[i]; // Add current expense to total
i++;
}

cout << "Total Expenses: " << total << endl;

return 0;
}
Explanation: The while loop continues until i is less than 7, summing up the values in the expenses
array.
3. Do-While Loop
Scenario: Find and print the maximum expense for the week.
C++ Code:
#include <iostream>
using namespace std;

int main() {
float expenses[7] = {45.50, 60.25, 55.75, 40.00, 75.10, 50.60, 30.00};
int i = 0;
float maxExpense = expenses[0];

GHSs(Chap-5 Array & Strings) Page 11


do {
if (expenses[i] > maxExpense) {
maxExpense = expenses[i]; // Update maxExpense if current expense is
greater
}
i++;
} while (i < 7);

cout << "Maximum Expense: " << maxExpense << endl;

return 0;
}
Explanation: The do-while loop iterates through the array, updating the maxExpense with the highest
value found.
4. Nested Loop
Scenario: A 2D array of weekly expenses for two weeks and print all values.
C++ Code:
#include <iostream>
using namespace std;

int main() {
float expenses[2][7] = {
{45.50, 60.25, 55.75, 40.00, 75.10, 50.60, 30.00}, // Week 1
{35.00, 45.75, 50.20, 55.30, 65.00, 40.00, 25.50} // Week 2
};

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


cout << "Week " << week + 1 << " Expenses: ";
for (int day = 0; day < 7; day++) {
cout << expenses[week][day] << " ";
}
cout << endl;
}

return 0;
}
Explanation: The nested for loop iterates through each week and each day, printing out all expenses
from the 2D array.
Sizes of Operators
In programming, especially in C++, operators are used to perform operations on variables and values.
Each operator can have different sizes, and it's important to understand this to ensure that operations are
performed correctly.
Real-Life Examples with Diagrams
1. Basic Arithmetic Operators
Scenario: Think of a kitchen where you measure ingredients using different-sized cups.
 Addition (+): Adding ingredients together (e.g., 1 cup of sugar + 2 cups of sugar).
 Subtraction (-): Removing a portion of ingredients (e.g., 3 cups of flour - 1 cup of flour).
 Multiplication (*): Scaling up recipes (e.g., 2 eggs * 3 recipes).
 Division (/): Dividing ingredients among servings (e.g., 10 cups of milk divided by 5 servings).
Diagram:
1 cup of sugar + 2 cups of sugar = 3 cups of sugar

GHSs(Chap-5 Array & Strings) Page 12


3 cups of flour - 1 cup of flour = 2 cups of flour
2 eggs * 3 recipes = 6 eggs
10 cups of milk / 5 servings = 2 cups of milk per serving
2. Assignment Operators
Scenario: Consider a warehouse where you store items and update the quantity.
 Simple Assignment (=): Storing an amount (e.g., 50 boxes of apples).
 Increment (+=): Adding more items (e.g., 50 boxes of apples + 10 boxes).
 Decrement (-=): Removing items (e.g., 50 boxes of apples - 5 boxes).
Diagram:
Initial stock: 50 boxes
Add 10 more: 50 boxes + 10 boxes = 60 boxes
Remove 5 boxes: 60 boxes - 5 boxes = 55 boxes
3. Comparison Operators
Scenario: Deciding whether you have enough supplies for a party.
 Equal to (==): Checking if the amount is exactly as needed (e.g., 30 cups of juice == 30 cups
needed).
 Not equal to (!=): Checking if the amount differs (e.g., 25 cups != 30 cups needed).
 Greater than (>): Checking if you have more than needed (e.g., 40 cups > 30 cups needed).
Diagram:
30 cups of juice == 30 cups needed (True)
25 cups != 30 cups needed (True)
40 cups > 30 cups needed (True)
4. Logical Operators
Scenario: Deciding whether a recipe is complete.
 AND (&&): Both conditions must be met (e.g., Have flour AND sugar).
 OR (||): Either condition can be met (e.g., Have flour OR sugar).
 **NOT (!) **: Checking the opposite condition (e.g., Not missing ingredients).
Diagram:
Have flour && Have sugar = Recipe complete
Have flour || Have sugar = Recipe might be incomplete
!Missing ingredients = All ingredients present
Code Examples
Example 1: Arithmetic Operators
Scenario: Calculate the total cost of items in a shopping list.
C++ Code:
#include <iostream>
using namespace std;

int main() {
float applePrice = 1.50;
float bananaPrice = 0.75;
int appleCount = 4;
int bananaCount = 6;

float totalCost = (applePrice * appleCount) + (bananaPrice * bananaCount);

cout << "Total cost: $" << totalCost << endl;

return 0;
}

GHSs(Chap-5 Array & Strings) Page 13


Explanation: Uses multiplication to calculate the cost of each type of fruit and addition to find the total
cost.
Example 2: Assignment Operators
Scenario: Track the number of items added to a cart.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int itemCount = 0;

itemCount += 3; // Adding 3 items


itemCount -= 1; // Removing 1 item

cout << "Total items in cart: " << itemCount << endl;

return 0;
}
Explanation: Uses += to add items and -= to remove items from the cart.
Example 3: Comparison Operators
Scenario: Check if a user’s age qualifies for a discount.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int age = 25;

if (age >= 18) {


cout << "Eligible for discount." << endl;
} else {
cout << "Not eligible for discount." << endl;
}

return 0;
}
Explanation: Uses >= to check if the age is 18 or more, qualifying for a discount.
Example 4: Logical Operators
Scenario: Check if a user can enter a club based on age and membership status.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int age = 21;
bool hasMembership = true;

if (age >= 18 && hasMembership) {


cout << "Entry allowed." << endl;
} else {
cout << "Entry denied." << endl;
}

return 0;

GHSs(Chap-5 Array & Strings) Page 14


}
Explanation: Uses && to ensure both conditions (age and membership) are met for entry.
Introduction to 2D Arrays
A 2D array is like a table or grid that contains data in two dimensions: rows and columns. Each element
in the array can be accessed using two indices: one for the row and one for the column. This type of
array is useful when dealing with data that naturally fits into a matrix format, such as a spreadsheet, a
chessboard, or a seating arrangement in a theater.
Example: Seating Arrangement in a Theater
Scenario: Imagine a theater with rows and columns of seats. Each seat can be identified by its row and
column number.
 Rows: Represented by the first index in the array.
 Columns: Represented by the second index in the array.
Diagram:
Column
0 1 2 3 4
+-----+-----+-----+-----+-----+
R0 | A1 | A2 | A3 | A4 | A5 | Row 0
+-----+-----+-----+-----+-----+
R1 | B1 | B2 | B3 | B4 | B5 | Row 1
+-----+-----+-----+-----+-----+
R2 | C1 | C2 | C3 | C4 | C5 | Row 2
+-----+-----+-----+-----+-----+
 R0C0 (Row 0, Column 0) corresponds to seat A1.
 R1C3 (Row 1, Column 3) corresponds to seat B4.
Concepts of 2D Arrays
 Declaration: In C++, a 2D array can be declared as follows:
int theaterSeats[3][5];
Here, 3 is the number of rows, and 5 is the number of columns.
 Initialization: A 2D array can be initialized with values at the time of declaration:
int theaterSeats[3][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
 Accessing Elements: Elements in a 2D array are accessed using row and column indices:
int seatNumber = theaterSeats[1][3]; // Accesses the value '9'
Real-Life Example 2: Chessboard Representation
Scenario: Consider a chessboard with 8 rows and 8 columns, where each position on the board can be
represented by a 2D array.
Diagram:
Column
0 1 2 3 4 5 6 7
+-----+-----+-----+-----+-----+-----+-----+-----+
R0 | R1 | N1 | B1 | Q | K | B2 | N2 | R2 | Row 0
+-----+-----+-----+-----+-----+-----+-----+-----+
R1 | P1 | P2 | P3 | P4 | P5 | P6 | P7 | P8 | Row 1
+-----+-----+-----+-----+-----+-----+-----+-----+
R2 | | | | | | | | | Row 2
+-----+-----+-----+-----+-----+-----+-----+-----+
R3 | | | | | | | | | Row 3
+-----+-----+-----+-----+-----+-----+-----+-----+

GHSs(Chap-5 Array & Strings) Page 15


R4 | | | | | | | | | Row 4
+-----+-----+-----+-----+-----+-----+-----+-----+
R5 | | | | | | | | | Row 5
+-----+-----+-----+-----+-----+-----+-----+-----+
R6 | P9 | P10 | P11 | P12 | P13 | P14 | P15 | P16 | Row 6
+-----+-----+-----+-----+-----+-----+-----+-----+
R7 | R3 | N3 | B3 | Q2 | K2 | B4 | N4 | R4 | Row 7
+-----+-----+-----+-----+-----+-----+-----+-----+
Here, R0C0 (Row 0, Column 0) might represent the position of a rook (R1), and R7C7 (Row 7, Column
7) might represent the position of another rook (R4).
Code Example 1: Seating Arrangement in a Theater
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array representing a theater seating arrangement
int theaterSeats[3][5] = {
{101, 102, 103, 104, 105},
{106, 107, 108, 109, 110},
{111, 112, 113, 114, 115}
};

// Accessing and printing a specific seat number


cout << "Seat number at Row 1, Column 3: " << theaterSeats[1][3] << endl;

// Looping through the array to print all seat numbers


cout << "All seat numbers in the theater:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout << theaterSeats[i][j] << " ";
}
cout << endl;
}

return 0;
}
Explanation: The code initializes a 2D array with seat numbers and then prints a specific seat number.
It also loops through the entire array to display all seat numbers.
Code Example 2: Chessboard Representation
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array representing a chessboard
char chessBoard[8][8] = {
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'}

GHSs(Chap-5 Array & Strings) Page 16


};

// Accessing and printing a specific piece position


cout << "Piece at Row 0, Column 3: " << chessBoard[0][3] << endl;

// Looping through the array to print the entire chessboard


cout << "Current chessboard layout:" << endl;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cout << chessBoard[i][j] << " ";
}
cout << endl;
}

return 0;
}
Explanation: This code initializes a 2D array to represent a chessboard. It prints the piece at a specific
position and loops through the entire array to display the chessboard layout.
Summary
 2D Arrays: Useful for storing data in a grid or matrix format, like seating arrangements or a
chessboard.
 Declaration and Initialization: A 2D array is declared with two indices and can be initialized
with values.
 Accessing Elements: Elements are accessed using row and column indices.

Definition of 2D Arrays
A 2D array, or two-dimensional array, is a type of array in which the data is organized in rows and
columns, forming a grid or matrix. Each element in this array can be accessed using two indices: one for
the row and one for the column.
Example: A Classroom Seating Chart
Scenario: Imagine a classroom where students are seated in rows and columns. Each seat can be
identified by a specific row and column number.
 Rows: The horizontal lines of seats.
 Columns: The vertical lines of seats.
Diagram:
Columns
0 1 2 3 4
+-----+-----+-----+-----+-----+
0 | S1 | S2 | S3 | S4 | S5 | Row 0
+-----+-----+-----+-----+-----+
1 | S6 | S7 | S8 | S9 | S10 | Row 1
+-----+-----+-----+-----+-----+
2 | S11 | S12 | S13 | S14 | S15 | Row 2
+-----+-----+-----+-----+-----+
In this example:
 S1 is at Row 0, Column 0.
 S9 is at Row 1, Column 3.
Initializing a 2D Array
In C++, a 2D array can be declared and initialized as follows:
int classroom[3][5] = {
{1, 2, 3, 4, 5},

GHSs(Chap-5 Array & Strings) Page 17


{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
Here:
 The array classroom has 3 rows and 5 columns.
 The first row contains elements 1, 2, 3, 4, 5.
 The second row contains elements 6, 7, 8, 9, 10.
 The third row contains elements 11, 12, 13, 14, 15.
Real-Life Example: Monthly Sales Data
Scenario: Consider a shop that tracks its monthly sales for different products. Each row in the array
represents a product, and each column represents sales data for a particular month.
Diagram:
Months
Jan Feb Mar Apr May
+-----+-----+-----+-----+-----+
P1| 100 | 150 | 120 | 130 | 160 | Product 1
+-----+-----+-----+-----+-----+
P2| 200 | 210 | 190 | 220 | 230 | Product 2
+-----+-----+-----+-----+-----+
P3| 300 | 310 | 320 | 330 | 340 | Product 3
+-----+-----+-----+-----+-----+
In this example:
 P1 represents Product 1 with sales data for each month.
 P2 represents Product 2, and so on.
Code Example 1: Classroom Seating Chart
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array representing a classroom seating chart
string classroom[3][5] = {
{"S1", "S2", "S3", "S4", "S5"},
{"S6", "S7", "S8", "S9", "S10"},
{"S11", "S12", "S13", "S14", "S15"}
};

// Accessing and printing a specific seat


cout << "Seat at Row 1, Column 3: " << classroom[1][3] << endl;

// Printing the entire seating chart


cout << "Classroom Seating Chart:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout << classroom[i][j] << " ";
}
cout << endl;
}

return 0;
}
Explanation: This code initializes a 2D array representing a classroom's seating chart and prints a
specific seat along with the entire chart.

GHSs(Chap-5 Array & Strings) Page 18


Code Example 2: Monthly Sales Data
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array representing monthly sales data
int sales[3][5] = {
{100, 150, 120, 130, 160},
{200, 210, 190, 220, 230},
{300, 310, 320, 330, 340}
};

// Accessing and printing sales data for Product 2 in March


cout << "Sales for Product 2 in March: " << sales[1][2] << endl;

// Printing the entire sales data table


cout << "Monthly Sales Data:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout << sales[i][j] << " ";
}
cout << endl;
}

return 0;
}
Explanation: This code initializes a 2D array representing monthly sales data for different products and
prints specific sales data along with the entire table.
Code Example 3: Temperature Data for Different Cities
Scenario: Suppose you have temperature data for three cities over a week.
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array representing temperature data for 3 cities over 7 days
float temperatures[3][7] = {
{30.5, 32.0, 31.8, 33.1, 30.0, 29.9, 32.5},
{25.2, 26.8, 27.1, 28.0, 24.9, 26.0, 27.5},
{20.1, 21.5, 22.3, 23.0, 19.8, 20.5, 21.9}
};

// Printing the temperature of the first city on the fourth day


cout << "Temperature for City 1 on Day 4: " << temperatures[0][3] << "°C" <<
endl;

// Printing the entire temperature data


cout << "Weekly Temperature Data for Cities:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
cout << temperatures[i][j] << " ";
}
cout << endl;
}

GHSs(Chap-5 Array & Strings) Page 19


return 0;
}
Explanation: This code demonstrates storing and accessing temperature data for three cities over seven
days using a 2D array.
Code Example 4: Multiplication Table
Scenario: Generate and display a multiplication table using a 2D array.
C++ Code:
#include <iostream>
using namespace std;

int main() {
// 2D array for multiplication table
int multiplicationTable[5][5];

// Filling the table


for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
multiplicationTable[i-1][j-1] = i * j;
}
}

// Printing the multiplication table


cout << "Multiplication Table (5x5):" << endl;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << multiplicationTable[i][j] << "\t";
}
cout << endl;
}

return 0;
}
Explanation: This code creates a 5x5 multiplication table using a 2D array and then prints it.

Accessing and Writing at an Index in a 2D Array


In a 2D array, you can think of the data as being stored in a grid, where each element has a specific
position defined by its row and column. Accessing and writing to this grid is like selecting or modifying
a specific cell in a table.
Example: Spreadsheet (Excel Table)
Scenario: Imagine a spreadsheet (like an Excel sheet) where each cell contains data. You can access a
cell to read its value or write a new value into it.
Diagram:
Columns
0 1 2
+-----+-----+-----+
0 | 10 | 20 | 30 | Row 0
+-----+-----+-----+
1 | 40 | 50 | 60 | Row 1
+-----+-----+-----+
2 | 70 | 80 | 90 | Row 2
+-----+-----+-----+

GHSs(Chap-5 Array & Strings) Page 20


 Accessing: To get the value at Row 1, Column 2, you look at the cell where Row 1 and Column
2 intersect, which is 60.
 Writing: If you want to change the value in Row 2, Column 1 from 80 to 100, you write 100 into
that cell.
Accessing and Writing in C++ 2D Arrays
In C++, you access or write to an element in a 2D array using its row and column indices.
Example 1: Accessing an Element
C++ Code:
#include <iostream>
using namespace std;

int main() {
int data[3][3] = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};

// Accessing the element at Row 1, Column 2


int value = data[1][2];
cout << "Value at Row 1, Column 2: " << value << endl;

return 0;
}
Explanation: This program accesses the element at Row 1, Column 2, which is 60, and displays it.
Example 2: Writing to an Element
C++ Code:
#include <iostream>
using namespace std;

int main() {
int data[3][3] = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};

// Writing a new value at Row 2, Column 1


data[2][1] = 100;

// Accessing and printing the updated value


cout << "Updated value at Row 2, Column 1: " << data[2][1] << endl;

return 0;
}
Explanation: This program changes the value at Row 2, Column 1 from 80 to 100 and displays the
updated value.
Example 3: Modifying a Specific Row
Scenario: Imagine updating the scores of a particular row in a table.
C++ Code:
#include <iostream>
using namespace std;

GHSs(Chap-5 Array & Strings) Page 21


int main() {
int scores[2][3] = {
{85, 90, 95},
{70, 75, 80}
};

// Updating all values in Row 0


for (int i = 0; i < 3; i++) {
scores[0][i] += 5; // Increasing each score by 5
}

// Printing the updated row


cout << "Updated scores in Row 0: ";
for (int i = 0; i < 3; i++) {
cout << scores[0][i] << " ";
}
cout << endl;

return 0;
}
Explanation: This code modifies all elements in Row 0 by increasing each score by 5, then prints the
updated row.
Example 4: Modifying a Specific Column
Scenario: Updating the sales data for a particular month.
C++ Code:
#include <iostream>
using namespace std;

int main() {
int sales[3][3] = {
{200, 250, 300},
{150, 180, 210},
{100, 120, 140}
};

// Updating all values in Column 1


for (int i = 0; i < 3; i++) {
sales[i][1] += 50; // Increasing each sales figure by 50
}

// Printing the updated column


cout << "Updated sales in Column 1: ";
for (int i = 0; i < 3; i++) {
cout << sales[i][1] << " ";
}
cout << endl;

return 0;
}
Explanation: This code modifies all elements in Column 1 by increasing each sales figure by 50, then
prints the updated column.
What are Strings?
A string is a sequence of characters, similar to a word or sentence in human language. In programming,
strings are used to represent text. For example, the word "Hello" is a string made up of the characters 'H',

GHSs(Chap-5 Array & Strings) Page 22


'e', 'l', 'l', 'o'. Strings are essential for handling text-based data, such as names, addresses, or any other
piece of information that consists of letters, numbers, or symbols.
Example: A Name Tag
Scenario: Imagine a name tag that you wear at a conference. The tag displays your name, which is a
string.
 String: "John Doe"
 Characters: 'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'
Diagram:
+-------------------+
| John Doe | <-- String
+-------------------+
In this example:
 The entire text "John Doe" is considered a string.
 Each individual letter, including the space, is a character.
Defining a String in C++
In C++, a string can be defined in two main ways:
1. Using a character array.
2. Using the std::string class.
Example 1: Character Array
A character array is an older way to define a string, where you explicitly define the array size and fill it
with characters.
#include <iostream>
using namespace std;

int main() {
char name[9] = {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e', '\0'}; // Character
array with a null terminator

cout << "Name: " << name << endl;

return 0;
}
Explanation: Here, we define a character array named name, which stores the string "John Doe". The \0
at the end marks the end of the string in C++.
Example 2: Using std::string
The std::string class is a more modern and flexible way to work with strings in C++. It allows you to
define and manipulate strings more easily.
#include <iostream>
#include <string>
using namespace std;

int main() {
string name = "John Doe"; // Using the string class

cout << "Name: " << name << endl;

return 0;
}
Explanation: This code defines a string using the std::string class, which simplifies working with
text data.

GHSs(Chap-5 Array & Strings) Page 23


Example: A Sentence in a Book
Scenario: Consider a sentence in a book. The sentence "The quick brown fox jumps over the lazy dog."
is a string.
Diagram:
+-------------------------------------------------+
| The quick brown fox jumps over the lazy dog. | <-- String
+-------------------------------------------------+
String Operations in C++
Example 3: Concatenating Strings
Concatenation is the process of joining two or more strings together.
#include <iostream>
#include <string>
using namespace std;

int main() {
string firstName = "John";
string lastName = "Doe";

// Concatenating strings
string fullName = firstName + " " + lastName;

cout << "Full Name: " << fullName << endl;

return 0;
}
Explanation: This code concatenates the firstName and lastName strings to create a fullName string.
Example 4: Finding Length of a String
You can find out how many characters are in a string using the .length() or .size() method.
#include <iostream>
#include <string>
using namespace std;

int main() {
string sentence = "The quick brown fox";

// Finding the length of the string


int length = sentence.length();

cout << "Length of sentence: " << length << " characters" << endl;

return 0;
}
Explanation: This code finds and prints the length of the sentence string, which is the number of
characters in the string.
Example 5: Accessing Individual Characters
You can access individual characters in a string by using an index, similar to accessing elements in an
array.
#include <iostream>
#include <string>
using namespace std;

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

GHSs(Chap-5 Array & Strings) Page 24


// Accessing individual characters
cout << "First letter: " << word[0] << endl;
cout << "Last letter: " << word[word.length() - 1] << endl;

return 0;
}
Explanation: This code accesses and prints the first and last characters of the string word.
Example 6: Modifying a String
You can change the contents of a string by modifying its characters.
#include <iostream>
#include <string>
using namespace std;

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

// Modifying the string


greeting[0] = 'J';

cout << "Modified greeting: " << greeting << endl;

return 0;
}
Explanation: This code changes the first character of the string greeting from 'H' to 'J', resulting in the
modified string "Jello".
Summary
 Strings: A sequence of characters used to represent text.
 Defining Strings: Can be defined using a character array or the std::string class.
 Real-Life Examples: Names, sentences, or any text-based data.
Initializing Strings
Initializing a string means assigning a value to a string variable at the time of its creation. In C++,
strings can be initialized in various ways, depending on how you want to use them.
Naming a File
Imagine you're saving a document on your computer. The document needs a name before you can save
it, such as "Report.docx." The name you give is like initializing a string.
Diagram:
+-----------------------------+
| Save As: "Report.docx" | <-- String Initialization
+-----------------------------+
In this example, "Report.docx" is the string that represents the name of the file.
Ways to Initialize Strings in C++
1. Using a Character Array
A character array is an older method to initialize a string in C++. You must manually define the size and
assign each character, including the null terminator \0 that marks the end of the string.
Example 1:
#include <iostream>
using namespace std;

int main() {

GHSs(Chap-5 Array & Strings) Page 25


char fileName[12] = {'R', 'e', 'p', 'o', 'r', 't', '.', 'd', 'o', 'c', 'x', '\
0'};
cout << "File Name: " << fileName << endl;

return 0;
}
Explanation: This code initializes a string using a character array. The string "Report.docx" is stored in
the array, and \0 is added to mark the end of the string.
2. Using String Literals
String literals are the most straightforward way to initialize a string. You simply assign the text directly
to the string variable.
Example 2:
#include <iostream>
#include <string>
using namespace std;

int main() {
string fileName = "Report.docx"; // String literal
cout << "File Name: " << fileName << endl;

return 0;
}
Explanation: Here, the string is initialized with a value directly, making the code easy to read and write.
3. Using the std::string Class and Input
You can initialize a string by getting input from the user or another source.
Example 3:
#include <iostream>
#include <string>
using namespace std;

int main() {
string fileName;
cout << "Enter file name: ";
cin >> fileName; // User inputs file name
cout << "File Name: " << fileName << endl;

return 0;
}
Explanation: This code allows the user to input the file name, which is then stored in the fileName
string.
4. Initializing an Empty String
You can initialize a string as empty, and later assign or modify its value.
Example 4:
#include <iostream>
#include <string>
using namespace std;

int main() {
string fileName; // Empty string initialization
fileName = "Report.docx"; // Assigning a value later

cout << "File Name: " << fileName << endl;

GHSs(Chap-5 Array & Strings) Page 26


return 0;
}
Explanation: The string fileName is initialized as an empty string and later assigned a value, showing
flexibility in how strings can be managed.
Example: Naming Different Files
Consider naming various files on your computer, like "Resume.pdf", "Invoice.xls", or
"Presentation.pptx". Each file name is a different string, initialized in various ways depending on how
you create or save the file.
Diagram:
+-----------------------------+
| Save As: "Resume.pdf" | <-- String Initialization
+-----------------------------+
| Save As: "Invoice.xls" | <-- String Initialization
+-----------------------------+
| Save As: "Presentation.pptx" | <-- String Initialization
+-----------------------------+
Each file name is a separate string, initialized with specific text that identifies the file.
Summary
 Initializing Strings: Assigning a value to a string when it is created.
 Ways to Initialize:
o Character Array: Manually defining each character.
o String Literals: Directly assigning text.
o User Input: Initializing through user-provided values.
o Empty Initialization: Starting empty and assigning a value later.

Commonly Used String Functions in C++


Strings are an essential part of programming, especially when dealing with text. In C++, several
functions allow you to manipulate strings effectively. Let’s explore some of the most commonly used
string functions with real-life analogies, diagrams, and code examples.
1. Length of a String (length() or size())
Example: Imagine counting the number of letters in your name to see how long it is.
Function: length() or size() is used to find out how many characters are in a string.
Diagram:
+-------------------+
| "John Doe" | <-- String
+-------------------+
|
v
+-------------------+
| Length = 8 | <-- Result
+-------------------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string name = "John Doe";
cout << "Length of name: " << name.length() << endl; // or name.size()
return 0;

GHSs(Chap-5 Array & Strings) Page 27


}
Explanation: The code calculates the length of the string "John Doe", which is 8 characters.
2. String Concatenation (+ or append())
Real-Life Example: Imagine writing a full name by combining a first name and a last name.
Function: The + operator or append() function is used to combine two strings.
Diagram:
+------------+ +------------+
| "John" | + | " Doe" | <-- Strings
+------------+ +------------+
|
v
+-------------------+
| "John Doe" | <-- Result
+-------------------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName; // Using + operator

cout << "Full Name: " << fullName << endl;

return 0;
}
Explanation: This code concatenates the first name "John" and the last name "Doe" to create the full
name "John Doe".
3. Accessing Characters in a String ([] operator)
Real-Life Example: Imagine you want to find out the first letter of your name.
Function: The [] operator is used to access individual characters in a string by their index.
Diagram:
+-------------------+
| "John Doe" | <-- String
+-------------------+
|
v
+-------+
| 'J' | <-- First Character
+-------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string name = "John Doe";
cout << "First letter of the name: " << name[0] << endl; // Accessing first
character

return 0;

GHSs(Chap-5 Array & Strings) Page 28


}
Explanation: The code accesses and prints the first character of the string "John Doe", which is 'J'.
4. String Comparison (compare())
Real-Life Example: Imagine comparing two words to see if they are the same, like checking if "apple"
is the same as "orange".
Function: The compare() function is used to compare two strings lexicographically (alphabetically).
Diagram:
+------------+ +------------+
| "apple" | ? | "orange" | <-- Strings
+------------+ +------------+
|
v
+-------------------+
| Result: Not Equal |
+-------------------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string word1 = "apple";
string word2 = "orange";

if (word1.compare(word2) == 0) {
cout << "The words are the same." << endl;
} else {
cout << "The words are different." << endl;
}

return 0;
}
Explanation: This code compares the two strings "apple" and "orange". Since they are different, the
output will be "The words are different."
5. Finding a Substring (find())
Example: Imagine searching for a specific word in a sentence, like finding the word "fox" in "The quick
brown fox jumps over the lazy dog."
Function: The find() function is used to search for a substring within a string and returns the index
where the substring starts.
Diagram:
+--------------------------------------------------+
| "The quick brown fox jumps over the lazy dog." | <-- String
+--------------------------------------------------+
|
v
+------------------------+
| Substring "fox" found | <-- Result
| at index 16 |
+------------------------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

GHSs(Chap-5 Array & Strings) Page 29


int main() {
string sentence = "The quick brown fox jumps over the lazy dog.";
string word = "fox";

size_t found = sentence.find(word);


if (found != string::npos) {
cout << "Found '" << word << "' at index: " << found << endl;
} else {
cout << "Word not found." << endl;
}

return 0;
}
Explanation: The code searches for the word "fox" in the sentence and finds it at index 16.
6. Replacing a Substring (replace())
Example: Imagine editing a document where you want to replace every instance of "cat" with "dog."
Function: The replace() function replaces a part of a string with another string.
Diagram:
+----------------------------+
| "I have a black cat." | <-- Original String
+----------------------------+
|
v
+----------------------------+
| "I have a black dog." | <-- After Replacement
+----------------------------+
Code Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string sentence = "I have a black cat.";
string oldWord = "cat";
string newWord = "dog";

size_t pos = sentence.find(oldWord);


if (pos != string::npos) {
sentence.replace(pos, oldWord.length(), newWord);
}

cout << "Updated sentence: " << sentence << endl;

return 0;
}
Explanation: The code finds the word "cat" in the sentence and replaces it with "dog," resulting in "I
have a black dog."
Summary
 Length of a String (length() or size()): Determines the number of characters in a string.
 String Concatenation (+ or append()): Combines two or more strings.
 Accessing Characters ([] operator): Retrieves individual characters from a string.
 String Comparison (compare()): Compares two strings to see if they are the same.

GHSs(Chap-5 Array & Strings) Page 30


 Finding a Substring (find()): Searches for a substring within a string.
 Replacing a Substring (replace()): Replaces part of a string with another string.

END OF CHAP-5 (Array & Strings)

GHSs(Chap-5 Array & Strings) Page 31

You might also like