0% found this document useful (0 votes)
4 views13 pages

Ds Assignment

The document contains a series of C++ lab assignments focused on string operations, including index, substring, insert, delete, and replace functionalities. Each question provides a code implementation along with example usage and output. The code demonstrates basic string manipulation techniques using arrays and functions.
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)
4 views13 pages

Ds Assignment

The document contains a series of C++ lab assignments focused on string operations, including index, substring, insert, delete, and replace functionalities. Each question provides a code implementation along with example usage and output. The code demonstrates basic string manipulation techniques using arrays and functions.
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/ 13

Lab Assignment 01

Question 1:
Implement Index Operation.
#include <iostream>
using namespace std;
class Solution
{
public:
int index(const char text[], const char pat[])
{
int textLength = 0, patLength = 0;
int i, j;
while (text[textLength] != '\0')
textLength++;
while (pat[patLength] != '\0')
patLength++;
for (i = 0; i <= textLength - patLength; i++)
{
for (j = 0; j < patLength; j++)
{
if (text[i + j] != pat[j])
break;
}
if (j == patLength)
return i + 1;
}
return 0;
}
};
int main()
{
Solution s1;
char text[] = "His Father is the Professor.";
char pattern1[] = "the";
char pattern2[] = "then";
char pattern3[] = "otheo";
cout << "INDEX(T, '" << pattern1 << "') = " <<
s1.index(text, pattern1) << endl;
cout << "INDEX(T, '" << pattern2 << "') = " <<
s1.index(text, pattern2) << endl;
cout << "INDEX(T, '" << pattern3 << "') = " <<
s1.index(text, pattern3) << endl;
return 0;
}
OUTPUT

Question 2:
Implement Substring Operation.
#include <iostream>
#include <cstring>
using namespace std;
class Solution
{
public:
void SUBSTRING(const char text[], int initial, int len)
{
if (initial < 0 || initial >= strlen(text) || len < 0 || len
> strlen(text))
cout << "Invalid substring parameters." << endl;
else
{
for (int i = initial - 1; i < initial + len - 1; i++)
{
cout << text[i];
}
}
cout << endl;
}
};
int main()
{
Solution s1;
char text[] = "TO BE OR NOT TO BE";
int intial = 4, len = 7;
cout << "SUBSTRING(\"" << text << "\", " << intial
<< "," << len << ") = ";
s1.SUBSTRING(text, intial, len);
intial = 100, len = 5;
cout << "SUBSTRING(\"" << text << "\", " << intial
<< "," << len << ") = ";
s1.SUBSTRING(text, intial, len);
return 0;
}

OUTPUT
Question 3:
Implement Insert Operation.
#include <iostream>
#include <cstring>
using namespace std;
class Solution
{
public:
void INSERT(char text[], int pos, char add[])
{
if (pos < 1 || pos >= strlen(text) + 1)
{
cout << "Invalid Position." << endl;
return;
}
for (int i = strlen(text); i >= pos - 1; i--)
{
text[i + strlen(add)] = text[i];
}
for (int i = 0; i < strlen(add); i++)
{
text[i + (pos - 1)] = add[i];
}
cout << text << endl;
}
};
int main()
{
Solution s1;
char text[] = "ABCDEFG";
char insText[] = "XYZ";
int pos = 0;
cout << "INSERT(\"" << text << "\"" << "," << pos
<< "," << "\"" << insText << "\" ) = ";
s1.INSERT(text, pos, insText);
pos = 3;
char text1[] = "ABCDEFG";
char insText1[] = "XYZ";
cout << "INSERT(\"" << text1 << "\"" << "," << pos
<< "," << "\"" << insText1 << "\" ) = ";
s1.INSERT(text1, pos, insText1);
return 0;
}

OUTPUT

Question 4:
Implement Delete Operation.
#include <iostream>
#include <cstring>
using namespace std;
class Solution
{
public:
void DELETE(char text[], int pos, int len)
{
if (pos < 1 || pos > strlen(text))
{
cout << text << endl;
return;
}
int i, j;
for (i = pos - 1, j = (pos - 1) + len; text[j] != '\0'; i+
+, j++)
{
text[i] = text[j];
}
text[i] = '\0';
cout << text << endl;
}
};
int main()
{
Solution s1;
char text[] = "ABCDEFG";
int pos = 3, len = 2;
cout << "Original Text :" << text << endl;
cout << "DELETE(\"" << text << "," << pos << "," <<
len << ")=";
s1.DELETE(text, pos, len);
char text1[] = "ABCDEFG";
pos = 0, len = 2;
cout << "Original Text :" << text1 << endl;
cout << "DELETE(\"" << text1 << "," << pos << ","
<< len << ")=";
s1.DELETE(text1, pos, len);
return 0;
}

OUTPUT

Question 5:
Implement Replace Operation.
#include <iostream>
#include <cstring>
using namespace std;
class Solution
{
public:
int INDEX(const char text[], char P1[])
{
int i, j;
for (i = 0; i < strlen(text) - strlen(P1); i++)
{
for (j = 0; j < strlen(P1); j++)
{
if (text[i + j] != P1[j])
break;
}
if (j == strlen(P1))
return i + 1;
}
return 0;
}
void Delete(char text[], int pos, int len)
{
int i, j;
if (pos < 1 || pos > strlen(text))
return;
for (i = pos - 1, j = i + len; text[j] != '\0'; i++, j++)
{
text[i] = text[j];
}
text[i] = '\0';
}
void INSERT(char text[], int pos, char P2[])
{
if (pos < 1 || pos > strlen(text) + 1)
return;
for (int i = strlen(text); i >= pos - 1; i--)
{
text[i + strlen(P2)] = text[i];
}
for (int i = 0; i < strlen(P2); i++)
{
text[i + (pos - 1)] = P2[i];
}
}
void Replace(char text[], char P1[], char P2[])
{
int ind = INDEX(text, P1);
Delete(text, ind, strlen(P1));
INSERT(text, ind, P2);
cout << text << endl;
}
};
int main()
{
Solution s1;
char text[] = "XABYABZ";
char p1[] = "AB", p2[] = "C";
cout << "REPLACE(\"" << text << "\"," << p1 << ","
<< p2 << ")=";
s1.Replace(text, p1, p2);
char text1[] = "XABYABZ";
char p1a[] = "XABY", p2a[] = "C";
cout << "REPLACE(\"" << text1 << "\"," << p1a <<
"," << p2a << ")=";
s1.Replace(text1, p1a, p2a);
return 0;
}

OUTPUT

You might also like