GCUF
SUBMITTED TO: DR.
AWAIS
SUBMITTED BY:
NAME: MUBASHRA BASHIR
ROLL NO: 234267
ASSIGNMENT: DSA (LAB)
TOPIC:
• INDEX OPERATION
• SUBSTRING OPERATION
• INSERT OPERATION
• DELETE OPERATION
• REPLACE OPERATION
SEMSETER:
3RD(EVENING)
Assignment
Q1. Implement the Index operation .
Solution:
We will create a function index in a class CharArray to find the first occurrence of a character or
substring in the array.
Code:
#include <iostream>
#include <cstring> using
namespace std; class
CharArray { private:
char* arr;
int length;
public:
CharArray(const char* input) {
length = strlen(input); arr =
new char[length + 1];
strcpy(arr, input); // Initialize array with input
}
~CharArray() {
delete[] arr; // Destructor to clean memory
}
Q1: Index function to find the first occurrence of a substring
int index(const char* sub) {
int subLen = strlen(sub);
for (int i = 0; i <= length - subLen; i++) {
bool found = true;
for (int j = 0; j < subLen; j++) {
if (arr[i + j] != sub[j]) {
found = false;
break;
}
}
if (found) {
return i; // Return the starting index of the substring
}
}
return -1; // Not found
}
void print() const {
cout << arr << endl;
}
};
int main() {
CharArray text("Hello World!");
// Find the index of the substring "World" int
index = text.index("World");
cout << "Index of 'World': " << index << endl;
return 0;
}
Output:
Index of 'World': 6
Q2. Implement the Substring operation
Solution:
We will implement a function subString in the CharArray class that extracts part of the array
and returns a new CharArray object.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class CharArray { private:
char* arr;
int length;
public:
CharArray(const char* input) {
length = strlen(input); arr =
new char[length + 1];
strcpy(arr, input); // Initialize array with input
}
~CharArray() {
delete[] arr; // Destructor to clean memory
}
Q2: SubString function to extract part of the array
CharArray* subString(int start, int subLen) {
if (start < 0 || start >= length || subLen <= 0 || start + subLen > length) {
return nullptr; // Invalid range }
char* subArr = new char[subLen + 1];
for (int i = 0; i < subLen; i++)
{ subArr[i] = arr[start + i];
}
subArr[subLen] = '\0'; // Null-terminate the substring
return new CharArray(subArr); // Return a new CharArray object
}
void print() const {
cout << arr << endl;
}
};
int main() {
CharArray text("Hello World!");
// Extract the substring "Hello" from the original array
CharArray* sub = text.subString(0, 5);
cout << "Substring (0, 5): "; sub-
>print();
delete sub; // Clean up memory for the substring object
return 0;
}
Output:
Substring (0, 5): Hello
Q3. Implement the Insert operation
Solution:
We will implement the insert function in the CharArray class, which inserts new characters into
the array at the specified position.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class CharArray { private:
char* arr;
int length;
public:
CharArray(const char* input) {
length = strlen(input); arr =
new char[length + 1];
strcpy(arr, input); // Initialize array with input
}
~CharArray() {
delete[] arr; // Destructor to clean memory
}
Q3: Insert function to insert a substring at a given position
void insert(const char* insertArr, int position) {
int insertLen = strlen(insertArr); if (position
< 0 || position > length) { return; //
Invalid position
}
char* newArr = new char[length + insertLen + 1];
// Copy elements before insertion point
for (int i = 0; i < position; i++)
{ newArr[i] = arr[i];
}
// Insert the new elements for
(int i = 0; i < insertLen; i++)
{ newArr[position + i] =
insertArr[i];
}
// Copy the remaining original elements
for (int i = position; i < length; i++)
{ newArr[insertLen + i] = arr[i];
}
newArr[length + insertLen] = '\0'; // Null-terminate the array
// Replace old array with the new
one delete[] arr; arr = newArr;
length += insertLen;
}
void print() const {
cout << arr << endl;
}
};
int main() {
CharArray text("Hello World!");
// Insert "Beautiful " at position 6 text.insert("Beautiful ", 6); cout << "After insertion: ";
text.print();
return 0; }
Output:
After insertion: Hello Beautiful World!
Q4. Implement the Delete operation .
Solution:
We will implement the deleteSub function in the CharArray class, which removes a portion of
the array.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class CharArray { private:
char* arr;
int length;
public:
CharArray(const char* input) {
length = strlen(input); arr =
new char[length + 1];
strcpy(arr, input); // Initialize array with input
}
~CharArray() {
delete[] arr; // Destructor to clean memory
}
Q4: Delete function to remove a range of characters
void deleteSub(int position, int subLen) {
if (position < 0 || position + subLen > length || subLen <= 0) {
return; // Invalid range
}
char* newArr = new char[length - subLen + 1];
// Copy characters before the deletion point
for (int i = 0; i < position; i++)
{ newArr[i] = arr[i];
}
// Copy characters after the deleted portion
for (int i = position + subLen; i < length; i++) {
newArr[i - subLen] = arr[i];
}
newArr[length - subLen] = '\0'; // Null-terminate the new array
// Replace old array with the new
one delete[] arr; arr = newArr;
length -= subLen;
}
void print() const {
cout << arr << endl;
}
};
int main() {
CharArray text("Hello Beautiful World!");
// Delete "Beautiful " from the
array text.deleteSub(6, 10); cout
<< "After deletion: ";
text.print();
return 0;
}
Output:
After deletion: Hello World!
Q5. Implement the Replace operation.
Solution:
We will implement the replace function in the CharArray class. It will first delete the old
substring and then insert the new substring at the same position.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class CharArray { private:
char* arr;
int length;
public:
CharArray(const char* input) { length =
strlen(input); arr = new char[length + 1];
strcpy(arr, input); // Initialize array with input
~CharArray() { delete[] arr; // Destructor
to clean memory
}
// Q4: Delete function to remove a range of characters void deleteSub(int position, int subLen) {
if (position < 0 || position + subLen > length || subLen <= 0) { return; // Invalid range
char* newArr = new char[length - subLen + 1];
// Copy characters before the deletion point
for (int i = 0; i < position; i++) { newArr[i] = arr[i];
// Copy characters after the deleted portion for (int i = position + subLen; i <
length; i++) { newArr[i - subLen] = arr[i];
newArr[length - subLen] = '\0'; // Null-terminate the new array
// Replace old array with the new one
delete[] arr; arr = newArr; length -= subLen;
// Q5: Replace function to replace a substring with another substring
void replace(const char* oldSub, const char* newSub) {
int index = this->index(oldSub); // Find the index of old substring
if (index != -1) { int oldLen = strlen(oldSub);
this->deleteSub(index, oldLen); // Delete the old substring
this->insert(newSub, index); // Insert the new substring
}
}
// Index function to find the first occurrence of a substring
int index(const char* sub) { int subLen = strlen(sub);
for (int i = 0; i <= length - subLen; i++) { bool found =
true; for (int j = 0; j < subLen; j++) { if (arr[i +
j] != sub[j]) { found = false; break;
}
}
if (found) {
return i; // Return the starting index of the substring
}
}
return -1; // Not found
}
void print() const
{ cout << arr << endl;
}
};
int main() {
CharArray text("Hello Beautiful World!");
// Replace "Beautiful" with "Amazing"
text.replace("Beautiful", "Amazing"); cout
<< "After replacement: ";
text.print();
return 0;
Output:
After replacement: Hello Amazing World!