Name: Rao Humza Majeed
Class: 02-132212-010
Question:
1) Find the length of the string (the string should be inserted by the user).
2) Determine the substring (from the inserted string) by taking the starting
index from the user and number of characters for the substring.
3) Take 2 strings as an input and concatenate them.
4) Delete the string (taken from the user) from the array of strings that was
inserted by the user.
5) Implement the Brute Force Search Algorithm.
CODE:
#include <iostream>
using namespace std;
int main() {
cout << "Enter a string: ";
string str;
cin >> str;
cout << "Length of the string: " << str.length() << endl;
cout << "Enter the starting index: ";
int start;
cin >> start;
cout << "Enter the number of characters: ";
int numChars;
cin >> numChars;
cout << "Substring: " << str.substr(start, numChars) << endl;
cout << "Enter two strings to concatenate: ";
string str1, str2;
cin >> str1 >> str2;
string concatenated = str1 + str2;
cout << "Concatenated string: " << concatenated << endl;
cout << "Enter the number of strings in the array: ";
int n;
cin >> n;
string arr[n];
cout << "Enter the strings: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
cout << "Enter the string to delete: ";
string toDelete;
cin >> toDelete;
for (int i = 0; i < n; i++) {
if (arr[i] == toDelete) {
for (int j = i; j < n-1; j++) {
arr[j] = arr[j+1];
n--;
break;
}
}
cout << "Array after deleting " << toDelete << ": ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
cout << endl;
cout << "Enter the string to search: ";
string searchString;
cin >> searchString;
cout << "Enter the text to search in: ";
string text;
cin >> text;
bool found = false;
for (int i = 0; i <= text.length() - searchString.length(); i++) {
bool match = true;
for (int j = 0; j < searchString.length(); j++) {
if (text[i+j] != searchString[j]) {
match = false;
break;
if (match) {
cout << "Match found at index " << i << endl;
found = true;
}
}
if (!found) {
cout << "Match not found" << endl;
return 0;
}
OUTPUT: