0% found this document useful (0 votes)
20 views5 pages

Dsa Lab7

The document contains code to implement string operations like finding length, substring, concatenation and deletion from an array. It also implements a brute force search algorithm to find a substring in a given text.

Uploaded by

raonetflix1999
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)
20 views5 pages

Dsa Lab7

The document contains code to implement string operations like finding length, substring, concatenation and deletion from an array. It also implements a brute force search algorithm to find a substring in a given text.

Uploaded by

raonetflix1999
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/ 5

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy