0% found this document useful (0 votes)
6 views

DSA Lab 01

Uploaded by

hanzala.bsce33
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)
6 views

DSA Lab 01

Uploaded by

hanzala.bsce33
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/ 10

Name : Muhammad Zain Tariq

Reg # 04-FET/BSCE/F22

Course: Data Structures and Algorithms Lab

SUBMITTED TO: Dr Baber Jadoon

DATE: 27-05-2024
Lab Number : 01
Implement following functions in a String Class:
String ToUpper()
String ToLower()
Int Length()
bool Search ( String s);
String SubString(int start, char delim);
String Operator + (String s2);

Make code of all the functions mentioned above and Write a


program using these function to separate WeekDay, Month, Day,
Year, Hours, Minutes and Seconds from a String “Sunday, August
14, 2022 14:15:16”

Code :
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX = 100;

class String {
private:
char str[MAX];
// int size; // to keep track of length of string
public:
String();
String(const char*);
void Insert(int pos, char ch);
void Del(int pos);
void Print();
void Println();
int Search(char ch);
String SubString(int start, int end);

String ToUpper(); // Converts all characters to Upper Case


String ToLower(); // Converts all characters to Lower Case
int Length(); // Returns length of the String
bool Search(String s); // search a string s
// within a String Object

String SubString(int start, char delim); // Returns a Substring


// until a delimiter character is found

String operator + (String s2); // Concatenates two Strings using


// + Operator Overloading

};
String::String() {
str[0] = '\0';
}
String::String(const char* s) {
int i = 0;
for (; s[i]; i++) {
str[i] = s[i];
}
str[i] = '\0';
}
// inserts a character at given position position
void String::Insert(int pos, char ch) {
// Shift elements to right
int i = MAX - 1;
for (; i > pos; i--)
str[i] = str[i - 1];
str[i] = ch;
}

// deletes an element from the given position position


void String::Del(int pos) {
// skip to the desired position
int i = pos;
for (; str[i]; i++)
str[i] = str[i + 1];
}

// searches String for a given element character


int String::Search(char ch) {
// Traverse the String
int i = 0;
for (; i < MAX; i++) {
if (str[i] == ch) {
return i; // return character position
}
}
return -1;
}

// Returns Substring
String String::SubString(int start, int total_characters) {
String temp;
int i = start;
int end = start + total_characters;
for (; i < end && i < MAX; i++) {
temp.str[i - start] = str[i];
}
temp.str[i - start] = '\0';
return temp;
}

// displays the contents of a String


void String::Print() {
cout << str;
}

void String::Println() {
cout << str << endl;
}

String String::ToUpper() {
for (int i = 0; str[i] != '\0'; ++i) {
if (str[i] >= 'a' && str[i] <= 'z') {
// Convert lowercase letters to uppercase
str[i] = str[i] - 'a' + 'A';
}
}
return *this; // returning copy of String
}

String String::ToLower() {
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32; // Convert to lowercase using ASCII
code
}
}
return *this;// returning copy of String
}

int String::Length() {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}

bool String::Search(String s) {
int i = 0;

for (; i < MAX; i++)


if (str == s.str) {
return i;
}
return -1;
}

String String::SubString(int start, char delim) {


String temp;
int i = start;
int j = 0;
while (str[i] != delim && str[i] != '\0') {
temp.str[j++] = str[i++];
}
temp.str[j] = '\0'; // Null-terminate the substring
return temp;
}

String String::operator+(String s2) {


String temp;

if (strlen(str) + strlen(s2.str) < MAX) {


strcpy_s(temp.str, str);
strcat_s(temp.str, s2.str);
}

else {
cout << "ERROR : String Overflow\n";
exit(1);
}

return temp;
}

int main() {
String s1("Sunday , August 14 , 2022 14:15:16");
s1.Println();
cout << "Length of S1 = " << s1.Length() << endl << endl;

String weekDay = s1.SubString(0, ',');


cout << " WeekDay = ";
weekDay.Println();

String month = s1.SubString(8, '1');


cout << " Month =";
month.Println();
String day = s1.SubString(15, ',');
cout << " Day =";
day.Println();

String year = s1.SubString(21, '1');


cout << " Year = ";
year.Println();

String hours = s1.SubString(25, ':');


cout << " Hours =";
hours.Println();

String minutes = s1.SubString(29, ':');


cout << " Minutes = ";
minutes.Println();

String seconds = s1.SubString(32, '33');


cout << " Seconds = ";
seconds.Println();

return 0;
}
Output :

You might also like