String type class : String « Data Type « C++
- C++
- Data Type
- String
String type class
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class StringClass {
char *p;
int len;
public:
StringClass(char *ptr)
{
len = strlen(ptr);
p = (char *) malloc(len+1);
if(!p) {
cout << "Allocation error\n";
exit(1);
}
strcpy(p, ptr);
}
~StringClass() {
cout << "Freeing p\n"; free(p);
}
void show()
{
cout << p << " - length: " << len;
cout << endl;
}
};
int main()
{
StringClass stringObject1("www.java2s.com"), stringObject2("www.java2s.com");
stringObject1.show();
stringObject2.show();
return 0;
}
Related examples in the same category
1. | A filter to remove white-space characters at the ends of lines. | | |
2. | A string demonstration: assignment, concatenate, compare | | |
3. | Demonstrate insert(), erase(), and replace(). | | |
4. | Use string: find, string::npos | | |
5. | Strings: size, iterator, count, begin and end | | |
6. | string: find( ) and rfind( ) | | |
7. | Print a name in two different formats | | |
8. | Several string operations: substr | | |
9. | String Char Indexing | | |
10. | String Find and replace | | |
11. | String Size | | |
12. | String SizeOf | | |
13. | A short string demonstration | | |
14. | String insert(), erase(), and replace() | | |
15. | string variable instead of a character array | | |
16. | Inputting Multiple Words into a String | | |
17. | Adding Strings | | |
18. | Read string from console | | |
19. | Insert, search, and replace in strings. | | |
20. | Accessing Characters In Strings | | |