Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- class String
- {
- char * pt;
- int len;
- public:
- String(int l)
- {
- len = l;
- pt = new char[len];
- }
- ~String()
- {
- delete [] pt;
- }
- String (const String& st)
- {
- len = st.len;
- pt = new char[len];
- for (int i = 0; i<len; i++)
- pt[i] = st.pt[i];
- }
- char get(int index)
- {
- if (index >= len || index < 0)
- return 0;
- else
- return pt[index];
- }
- int put(int index, char c)
- {
- if (index >= len || index < 0)
- return -1;
- else
- pt[index] = c;
- }
- int getLength()
- {
- return len;
- }
- void display()
- {
- for (int i = 0; i<len; i++)
- printf("%c", pt[i]);
- printf("\n");
- }
- friend void compare(String s1, String s2);
- };
- void compare(String s1, String s2)
- {
- if (s1.getLength() > s2.getLength())
- cout << "The first string is greater"<<endl;
- else if (s1.getLength() < s2.getLength())
- cout << "The second string is greater"<<endl;
- else
- cout << "Both are equal"<<endl;
- }
- int main()
- {
- String s1(4);
- String s2(5);
- String s3(5);
- s1.put(0, 'A');
- s1.put(1, 'B');
- s1.put(2, 'C');
- s1.put(3, 'D');
- s1.display(); //ABCD
- s2.put(0, '1');
- s2.put(1, '2');
- s2.put(2, '3');
- s2.put(3, '4');
- s2.display(); //1234
- compare(s1, s2);
- compare(s2, s1);
- compare(s2, s3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement