0% found this document useful (0 votes)
8 views4 pages

ASSIGNMENT No3

The document contains an assignment on polymorphism in C++, prepared by Hurr Abbas for a course in Object-Oriented Programming. It includes three programming tasks: sorting numeric and string arrays, removing the second letter from a string, and replacing the first occurrence of 'x' with 'y' in a string. Each task is accompanied by C++ code examples demonstrating the required functionality.

Uploaded by

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

ASSIGNMENT No3

The document contains an assignment on polymorphism in C++, prepared by Hurr Abbas for a course in Object-Oriented Programming. It includes three programming tasks: sorting numeric and string arrays, removing the second letter from a string, and replacing the first occurrence of 'x' with 'y' in a string. Each task is accompanied by C++ code examples demonstrating the required functionality.

Uploaded by

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

ASSIGNMENT No.

POLYMORPHISM IN C++

PREPARED BY
HURR ABBAS
SUBMITTED TO
Ms. AYESHA
ROLL No. 5111323030
FIELD: BS-AI-II

SUBJECT: OOP

IBADAT INTERNATIONAL UNIVERSITY, ISLAMABAD


Department of Computer Science & Information Technology
1. Write a c++ program to sort a numeric array and a string array
#include <iostream>
#include <string>
using namespace std;
// Function to sort a numeric array
void sortNumericArray(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
// Function to sort a string array
void sortStringArray(string strArr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (strArr[j] > strArr[j + 1]) {
swap(strArr[j], strArr[j + 1]);
}
}
}
}

int main() {
int numericArr[] = {5, 2, 9, 1, 7};
string stringArr[] = {"apple", "banana", "cherry", "date", "fig"};
int n = sizeof(numericArr) / sizeof(numericArr[0]);
sortNumericArray(numericArr, n);
sortStringArray(stringArr, n);
cout << "Sorted numeric array: ";
for (int i = 0; i < n; ++i) {
cout << numericArr[i] << " ";
}
cout << endl;

cout << "Sorted string array: ";


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

return 0;
}

2. Write program to remove 2nd letter. Let the input string is pwsxtpbcderxrtxgt then
the output is pwxtpbcderxrtxgt.
#include <iostream>
#include <string>
using namespace std;

string removeSecondLetter(const string& input) {


string output;
for (size_t i = 0; i < input.length(); ++i) {
if (i != 1) { // Skip the 2nd letter
output += input[i];
}
}
return output;
}

int main() {
string inputString = "pwsxtpbcderxrtxgt";
string result = removeSecondLetter(inputString);
cout << "Output after removing 2nd letter: " << result << endl;
return 0;
}

3. Read a string and replace first x by y. Input is artxuxtxxp and resulted output
artyuxtxxp.
#include <iostream>
#include <string>
using namespace std;

string replaceFirstXWithY(const string& input) {


string output = input;
size_t pos = output.find('x');
if (pos != string::npos) {
output[pos] = 'y';
}
return output;
}

int main() {
string inputString = "artxuxtxxp";
string result = replaceFirstXWithY(inputString);
cout << "Output after replacing first 'x' with 'y': " << result << endl;
return 0;
}

You might also like