#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

class objeto {
public:
virtual objeto *clonar()=0;
};

class cadena_m {
private:
char *c;
public:
cadena_m(char *s) {
c = new char[strlen(s)+1];
strcpy(c,s);
c[strlen(s)+1] = '\0';
}
~cadena_m() { delete[] c;}
char *valor () {return c;}
void cambiar (char *s) {
char *g = new char[strlen(s)+1];
strcpy(g, s);
g[strlen(s)] = '\0';
delete[] c;
c=g;
}
void concatenar (cadena_m *s) {
char *h = new char[strlen(s->c)+strlen(c)+1];
strcpy(h, c);
strcat(h, s->c);
h[strlen(s->c)+strlen(c)] = '\0';
delete[] c;
c=h;
}
int longitud () {
return strlen(c);
}
void a_minuscula() {
for (int i=0;i<longitud();i++) {
c[i] = tolower(c[i]);
}
}
void a_mayuscula() {
for (int i=0;i<longitud();i++) {
c[i] = toupper(c[i]);
}
}
char pos(int j) {
return c[j];
}
void reemplazar (cadena_m b, cadena_m r) {
char *b_aux=b.valor(), *r_aux=r.valor(), *cab=c, *b_cab=b_aux, *r_cab=r_aux;
int k, m, entries=0, len=longitud(), b_len=strlen(b_aux);
for (k=0;k<=len;k++) {
if (*(b_aux)==*c) {
b_aux++;
} else {
b_aux = b_cab;
}
if (*(b_aux)=='\0') {
*(c-b_len+1)= '°';
b_aux = b_cab;
entries++;
}
c++;
}
c=cab;
b_aux = b_cab;
char *temp = new char[len-(entries*b_len)+(entries*strlen(r_aux))];
cout<<len-(entries*b_len)+(entries*strlen(r_aux));
r_aux=r_cab;
k=0; m=0;
//defining two independent sub_ids to move temp and c//
temp[m] = '\0';
while (c[k]!='\0') {
//matching '\0' for concat/
if (c[k]=='°') {
strcat(temp, r_aux);
//temp must move the characters occupied by r, r:the replacement
//c must move the characters occupied by b, b:the base
m+=strlen(r_aux); c+=strlen(b_aux);
} else {
temp[m] = c[k];
m++; c++;
}
temp[m] = '\0';
}
//deleting memory//
delete[] c;
c=temp;
}
};

main(){
char *c = new char[20];
strcpy(c, "holasss");
c[6] = '\0';
cadena_m *s;
s = new cadena_m(c);
//
cadena_m u("ho");
cadena_m y("moalina");
//
cout<<s->valor();
cout<<s->longitud();
s->a_minuscula();
cout<<"\n\n"<<s->valor();
s->reemplazar(u, y);
cout<<"\n\n"<<s->valor();
cout<<s->longitud();
getche();
}