Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

OOP -ITU-2024 - String Class

Implement the class String which must perform the following tasks between various strings.

class Mystring 4. void Print() const;


{ s.Print();
int size;
5. int Length() const; // calculates length of a string
char *Vs; cout << s.Length(); //s= “This is Pakistan”; => 16
public:
6. static int Atoi(const MyString s); // convert string
1. Every possible constructor (with
to integer
Dynamically Allocated Memory of
int Atoi()const
cstring) and one destructor
7. static Mystring Itoa(int num); // Integer to
■ Empty
Alphabet conversion
■ char
■ Const char* p = “hello”
For example: For example:
■ Integer and a char (for
Mystring s1= “99” Mystring s1;
allocating memory to Vs with int val= s1.Atoi(); s1=s1.Itoa(123);
the passed character). // val+1 is 100 Mystring
■ integer (which will call itoa) ::Itoa(123)
// MyString s (123); // "123"
// Cs ⇒ {‘1’, ‘2’, ‘3’, ‘\0’}
// Size = 3
8. Trim removes ‘spaces’, ‘tabs’, ‘\n’ from the
// Provide necessary functionality beginning & end.
“ I am Pakistani ” ⇒ “I am Pakistani”
};
9. char operator[](int i)const;
returns copy of character at given i index
2. Shallow-Copy
First make a function MyString s1 = “Hello”;
ReplaceFirst(char c) which must char ch = s3[2]; //ch = l;
replace the first character of the
string by c. Then test Shallow Copy
10. char& operator[](int i);
MyString s1 = s1.Print() // zara
“Sara”; s2.Print() //zara
MyString s2 = s1; S1 = “This is a S1.Print(); // this is a
s1.ReplaceFirst(‘z’); Pakistan”; Pakistan
S1[0] = ‘t’;

3. Copy constructor (deep copy)


■ MyString(const MyString&)

MyString s1 = “Sara”; s1.Print() //


MyString s2 = s1; zara
s1.ReplaceFirst(‘z’); s2.Print()
//Sara
11. String equality // test whether two strings are equal or not

bool isEqual(const MyString& M);


bool isLess(const MyString& M);
bool isGreater(const MyString& M);

compare strings according to dictionary order

s1=”abcde” s1=”abcde” s1=”abcde”


s2=”abcde” s1=”zbcde” s2=”z”
cout<<s1.isEqual(s2); cout<<s1.isLess(s2); cout<<s2.isGreater(s1) ;
// evaluates true // evaluates true // evaluates true

12. MyString* Split(char delim, int & count) const;

Splitting around a character


This function takes a character and count by reference and returns a MyString* containing all
the addresses of the splitted strings and count - in how many parts the string is divided.

MyString s (“This is Pakistan”); SP’s => 0x100


MyString* SP’s = s.Split( ‘ ’, count ); 0x100 => MyString ( Cs=> | ’T’ | ’h’ | ’i’ | ’s’ | ’\0’ | .... , size = 4 )
//returns count = 3, SP’s pointing to array of 3 0x200 => MyString ( Cs=> | ’i’ | ’s’ | ’\0’ | .... , size = 2 )
MyStrings: 0x300 => MyString ( Cs=> | ’P’ | ’a’ | ’k’ | ’i’ | ’s’ | ’t’ | ’a’ | ’n’ |
’\0’ | ...., size=8 )

MyString s ("Pakistan"); int count; SP’s => 0x320


MyString* SP's = s.Split( 'i', count ); 0x320 => MyString ( Cs=> | ’P’ | ’a’ | ’k’ | ’\0’ | .... , size = 3 )
//returns count = 2, SP’s pointing to array of 2 0x400 => MyString ( Cs=> | ’s’ | ’t’ | ’a’ | ’n’ | ’\0’ | .... , size = 4
MyStrings: )

13. Tokenization //same as split, pass list of characters or string, returns a string array split based
on delimiters
■ Mystring* tokenize(const char* delim, int& count) const;
Tokenize (“, .”, count)
14. String concatenation //adds two strings and returns resultant string
■ Mystring concat(const Mystring& s2)const; // MyString s(“abc”), s2(“def”);
■ Mystring& append(const Mystring& s2); // MyString s3 = s.concat(s2);
// s.append(s2); s.print();

Mystring s1=”usama” , s2=” is great”


s3=s1+s2;
s1+=s2;

// s3 contains “usama is great”


// s1 is changed to “usama is great”

15. String All-substring searching:


This function takes a character string and count by reference. Returns an int* containing all the
indexes of the substring and count - # of times substring found.
MyString s (“This is Pakistan”);
int* All’s = s.AllSubStrings( “is”, count );
//returns count = 3, All’s pointing to array of 3 indices
(integers):
All’s => [0] 2, [1] 5, [2] 11

16. Find
■ First
■ Int find_first(char ch); // returns first index at which character is found
■ int find_first(const Mystring &) const; // returns first index at which string is found
■ Last
■ Int find_last(char ch); // returns last index at which character is found
■ int find_last(const Mystring&) const; // returns last index at which string is found
■ All
■ int* find_all(char ch, int &C); // returns pointer to an array of indices at which
character is present
■ int* find_all(const mystring &, int& C )const; // returns an array of indices at which
string is present
Mystring s1=”zabcdeaaa”;
int C;
int first= s1. find_first(‘a’); //first=1
int last=s1.find_last(‘a’); // last=8
int *indices= s1.find_all(‘a’, C); // indices contain {1,6,7,8} and C = 4

17. void remove_at(int i); //Removes a character at the given index

18. void insert_at(int i, char ch); // Add a character at given position

19. void insert_at(int i, const Mystring sub); // Add a string at given position

MyString s1=”apple” Mystring s1=”uama” ; Mystring s1=” is GREAT”,


; s1.insert_at(1,’s’); s2=”Usama”
s1.remove_at(3) ; //s1 becomes “usama” s1.insert_at(0,s2);
//s1 becomes // s1 becomes “Usama is GREAT”
“appe”

20. Remove character


■ First // remove only first occurrence of character
■ void remove_first(char ch);
■ Last // remove only last occurrence of character
■ void remove_last(char ch);
■ All // remove all occurrences of character
■ void remove_all(char ch);

Mystring s1=”sarfraz” Mystring s1=”sarfraz” Mystring s1=”sarfraz”


s1.remove_first(‘a’); s1.remove_last(‘a’); s1.remove_all(‘a’);
// s1 becomes “srfraz” // s1 becomes “sarfrz” // s1 becomes “srfrz”

21. void clear(); //clears the string

Mystring s1=”Sarfraz”
s1.clear()
//s1 becomes “”
22. ~MyString(); // Destructor

You might also like