






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







#include
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;
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();