Industrial Training

Tempstr



#include<iostream.h>
#include<conio.h>
#include<string.h>

 

template<class T>
int strlength(T *arr);

 

template<class T>
class string{
struct srep{
T *ptr; //pointer to te string
int sz; //size of the string
int refct; //reference count

srep(int size){ //constructor function
ptr=new T[size+1];
sz=size;
refct=0;
}
srep(srep &ref){ //copy constructor
ptr=new T[ref.sz+1];
sz=ref.sz;
int i;
for(i=0;i<=sz;i++)
ptr[i]=ref.ptr[i];
refct=0;
}
srep operator =(srep &a){ //overloaded assignment operator
int x;
x=a.sz;
srep b(x);
refct++;
return b;
}
void assign(T *str){ //assignment function
int i,strln;
strln=strlength(str);
for(i=0;i<strln;i++)
ptr[i]=str[i];
ptr[i]='\0';
sz=strln;
refct++;
}
int countref(void){ //returns the count of references to the string
return refct;
}
~srep(){ //destructor
cout<<"In destructor\n";
delete []ptr;
}
};
srep *alpha;

public:
string(int size); //constructor 1
string(T* intsr); //constructor 2 (parametrized)
string(string &ref); //copy constructor
void showstring(void); //displays the string
void assign1(T *ass); //assignment function
int substringof(string &big);//acertains whether the string is a substring of the string big.
//if true, 1 is returned,0 otherwise.
friend void concat(string &tar,string &src);
//concatenates string src to string tar
friend int compare(string &a,string &b);
//compares string a with string b

};

 

// ----------------- EXPLICIT fUNCTION DEFINITIONS -----------------------

 

template<class T>
string<T>::string(int size){//constructor 1
alpha=new srep(size);
}

template<class T>
string<T>::string(T *instr){//constructor 2
int len;
len=strlength(instr);
alpha=new srep(len);
alpha->assign(instr);
}

template<class T>
string<T>::string(string &ref){//copy constructor
int x;
x=(ref.alpha)->sz;
alpha=new srep(*(ref.alpha));
}

template<class T>
void string<T>::showstring(void){//displays the string
int i;
cout<<" String : ";
for (i=0;i<alpha->sz;i++)
cout<<alpha->ptr[i];
cout<<"\n Number of references to this string : "<<alpha->countref()<<endl;
return;
}

template<class T>
void string<T>::assign1(T *ass){//assignment function
alpha->assign(ass);
}

template<class T>
int string<T>::substringof(string &big){//is 'this' string a subset of the string big?
int res=0;//not a substring
int lbig,lsml,i,j,pos,state;

lbig=big.alpha->sz;
lsml=alpha->sz;
i=0;
while(i<lbig && state!=2){
j=0;
state=0;
if(big.alpha->ptr[i]==alpha->ptr[j])
pos=i;
while(big.alpha->ptr[i]==alpha->ptr[j]){
state=1;
if(alpha->ptr[i+1]=='\0'){
state=2;//match found
break;
}
i++;
j++;
}
i++;
}
if (state==2){
res=pos+1;//is a substring(match at position res)
cout<<"This string is a substring of the argument string.\n";
cout<<"Match found at position "<<res<<endl;
}

return res;
}

 

// --------------------------------------------------------------------
//====================================================
template<class T>
void concat(string<T> &tar,string<T> &src);
template<class T>
int compare(string<T> &a,string<T> &b);

 

int main(void){
unsigned char a[20];
string<unsigned char> g(10);
cout<<"ENTER VALUE TO BE ASSIGNED TO g\n";
cin>>a;
g.assign1(a);
cout<<"\n\t*'g.showstring();' //displaying g\n";
g.showstring();
string<unsigned char> h=g;
cout<<"\n\t*'string<unsigned char>h=g;' //copy constructor invoked\n\n\t*displaying h:\n";
h.showstring();
cout<<"\n\t*'concat(g,h);' //concatenation\n";
concat(g,h);
cout<<"\n\t*'compare(h,g);' //comparison\n";
compare(h,g);
cout<<"\n\t*'g.showstring()'\n";
g.showstring();
cout<<"\nENTER VALUE TO BE ASSIGNED TO g\n";
cin>>a;
g.assign1(a);
cout<<"\n\t*'h.substringof(g);' //finding substrings\n";
h.substringof(g);
cout<<endl<<endl;

return 0;
}

//====================================================

 

 

template<class T>
void concat(string<T> &tar,string<T> &src){//concatenates string src to the end of string tar
int i1,i2,i,cnt;
i1=tar.alpha->sz;
i2=src.alpha->sz;
T *arr;
arr=new T[i1+i2+1];
cnt=0;
for(i=0;i<i1;i++)
arr[cnt++]=tar.alpha->ptr[i];
for(i=0;i<i2;i++)
arr[cnt++]=src.alpha->ptr[i];
arr[cnt]='\0';
delete tar.alpha->ptr;
tar.alpha->ptr=arr;
tar.alpha->sz=i1+i2;
tar.alpha->refct++;
src.alpha->refct++;
return;

}

 

template<class T>
int compare(string<T> &a,string<T> &b){//compares the strings a and b
int i,j;
a.alpha->refct++;
b.alpha->refct++;

i=0;
j=0;
cout<<"Lexicographical comparizon:";
while(a.alpha->ptr[i]!='\0' && b.alpha->ptr[j]!='\0'){
if(a.alpha->ptr[i]<b.alpha->ptr[j]){
cout<<"string1 less than string2\n";
return -1;
}
if(a.alpha->ptr[i]>b.alpha->ptr[j]){
cout<<"string1 greater than string2\n";
return 1;
}
i++;
j++;
}
if(a.alpha->ptr[i]=='\0' && b.alpha->ptr[j]=='\0'){
cout<<"string1 identical to string2\n";
return 0;
}
if(a.alpha->ptr[i]=='\0'){// (and b is not..)
cout<<"string1 is less(shorter) than string2.\n";
return 1;
}
if(b.alpha->ptr[j]=='\0'){// (and a is not ..)
cout<<"string1 is greater(longer) than string2.\n";
return 1;
}

return -11;//no option is fired
}

 

 

template<class T>
int strlength(T arr[]){
int l=0;
while(arr[l]!='\0')
l++;
return l;
}

Hi I am Pluto.