Character String Class
//==========================================================
// CharString.h
//==========================================================
#include<iostream>
#include<string>
using namespace std;
#ifndef CHARSTRING_H
#define CHARSTRING_H
class charstring
{
private:
char* cstr;
int size;
public:
//constructers
charstring();
charstring(const char* src);
charstring(const char* src,int n);
charstring(const charstring& src);
charstring(const charstring& src,int n);
//operations
void copyfrom(const char* src);
void copyfrom(const char* src ,int n, int start=0);
void copyfrom(const charstring& src);
void copyfrom(const charstring& src ,int n, int start=0);
//substrings
charstring substring(int n, int start=0 )const;
int findsubstring(const char* str , int start =0 )const;
int findsubstring(const charstring* str , int start =0 )const;
//concatenation operations
string& concat(const char* str);
string& concat(const char* str ,int n , int start=0);
string& append(const charstring& str);
string& append(const charstring& str , int n ,int start =0);
//other operations
int getsize()const;
void reset();
void cutdown(int n);
void print()const;
};
#endif
//==========================================================
// CharString.cpp
//==========================================================
#include "charstring.h"
//constructers
charstring::charstring()
{
this->size=10;
this->cstr= new char[size];
for(int j=0; j<size ; j++)
this->cstr[j]='0';
}
charstring::charstring(const char* src)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++)
this->size=j+1;
this->cstr=new char[size];
for(int i=0; !(src[i]=='\0') ; i++)
this->cstr[i] = src[i];
}
charstring::charstring(const char* src,int n)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++)
this->size=j+1;
this->cstr=new char[size];
while(1)
{
if(n < this->size && n >= 0)
break;
cout<<"invalid input!!!\nsize to copy is greater than the contente size....enter again:"<<endl;
cin>>n;
}
for(int i=0 ; i < n ; i++)
this->cstr[i] = src[i];
this->size=n;
}
charstring::charstring(const charstring& src)
{
this->size=src.size;
cstr=new char[this->size];
for(int i=0 ; i < size ; i++)
this->cstr[i] = src.cstr[i];
}
charstring::charstring(const charstring& src,int n)
{
this->size=src.size;
cstr=new char[this->size];
while(1)
{
if(n < this->size && n >= 0)
break;
cout<<"invalid input!!!\nsize to copy is greater than the contente size....enter again:"<<endl;
cin>>n;
}
for(int i=0 ; i < (this->size) ; i++)
this->cstr[i] = src.cstr[i];
this->size=n;
}
//operations
void charstring::copyfrom(const char* src)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++);
if(j<=this->size)
{
int i=0;
for( ; !(src[i]=='\0') ; i++)
this->cstr[i]=src[i];
this->size=j;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const char* src ,int n, int start)
{
if(n<=this->size)
{
for( int i=start; (!(src[i]=='\0') || i < n ) ; i++)
this->cstr[i]=src[i];
this->size=n;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const charstring& src)
{
if(src.size< this->size)
{
for(int i=0 ; !(src.cstr[i]=='\0') ; i++)
this->cstr[i] = src.cstr[i];
this->size=src.size;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const charstring& src ,int n, int start)
{
if(n<this->size)
{
for( int i=start; (!(src.cstr[i]=='\0') || i < n) ; i++)
this->cstr[i] = src.cstr[i];
this->size=n;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
//substrings
charstring charstring::substring(int n, int start )const
{
int i=start;
charstring str="0000000000000000000000000";
for( ; ( i < n) ; i++)
str.cstr[i] = this->cstr[i];
return str;
}
int charstring::findsubstring(const char* str , int start )const
{
int i=start;
for( ; !(this->cstr[i]=='\0') ; i++)
if(this->cstr[i]==str[i])
{
return i;
}
return -1;
}
int charstring::findsubstring(const charstring* str , int start )const
{
int i=start;
for( ; !(this->cstr[i]=='\0') ; i++)
if(this->cstr[i]==str->cstr[i])
{
return i;
}
return -1;
}
//concatenation operations
string& charstring::concat(const char* str)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for( int j=0; !(str[j] == '\0') ; j++,i++)
{
strt[i]=str[j];
}
return strt;
}
string& charstring::concat(const char* str ,int n , int start)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j=start ; (!(str[j] == '\0') || j < n) ; j++,i++)
{
strt[i]=str[j];
}
return strt;
}
string& charstring::append(const charstring& str)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j= 0 ; (!(str.cstr[j] == '\0')) ; j++,i++)
{
strt[i]=str.cstr[j];
}
return strt;
}
string& charstring::append(const charstring& str , int n ,int start )
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j= start ; (!(str.cstr[j] == '\0') || j < n) ; j++,i++)
{
strt[i]=str.cstr[j];
}
return strt;
}
//other operations
int charstring::getsize()const
{
int i=0;
for( ; !(cstr[i]=='\0') ; i++);
return i;
}
void charstring::reset()
{
cout<<"all contents are reset to \"0\""<<endl;
for(int i=0 ; !(cstr[i]=='\0') ; i++)
this->cstr[i]='0';
this->size=0;
}
void charstring::cutdown(int n)
{
if(n < this->size)
{
int i=n+1;
this->cstr[n]='\0';
for( ;i <= size ; i++)
this->cstr[i]=0;
this->size=n;
}
else
{
cout<<"invalid size!!!\nno change in string"<<endl;;
}
}
void charstring::print()const
{
cout<<"-----------------------------"<<endl;
cout<<"size is:"<<this->size<<endl;
cout<<"contente of the string are:"<<endl;;
for(int i=0 ; i<this->size ; i++)
cout<<this->cstr[i]<<" ";
cout<<endl;
cout<<"*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*"<<endl<<endl;
}
// CharString.h
//==========================================================
#include<iostream>
#include<string>
using namespace std;
#ifndef CHARSTRING_H
#define CHARSTRING_H
class charstring
{
private:
char* cstr;
int size;
public:
//constructers
charstring();
charstring(const char* src);
charstring(const char* src,int n);
charstring(const charstring& src);
charstring(const charstring& src,int n);
//operations
void copyfrom(const char* src);
void copyfrom(const char* src ,int n, int start=0);
void copyfrom(const charstring& src);
void copyfrom(const charstring& src ,int n, int start=0);
//substrings
charstring substring(int n, int start=0 )const;
int findsubstring(const char* str , int start =0 )const;
int findsubstring(const charstring* str , int start =0 )const;
//concatenation operations
string& concat(const char* str);
string& concat(const char* str ,int n , int start=0);
string& append(const charstring& str);
string& append(const charstring& str , int n ,int start =0);
//other operations
int getsize()const;
void reset();
void cutdown(int n);
void print()const;
};
#endif
//==========================================================
// CharString.cpp
//==========================================================
#include "charstring.h"
//constructers
charstring::charstring()
{
this->size=10;
this->cstr= new char[size];
for(int j=0; j<size ; j++)
this->cstr[j]='0';
}
charstring::charstring(const char* src)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++)
this->size=j+1;
this->cstr=new char[size];
for(int i=0; !(src[i]=='\0') ; i++)
this->cstr[i] = src[i];
}
charstring::charstring(const char* src,int n)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++)
this->size=j+1;
this->cstr=new char[size];
while(1)
{
if(n < this->size && n >= 0)
break;
cout<<"invalid input!!!\nsize to copy is greater than the contente size....enter again:"<<endl;
cin>>n;
}
for(int i=0 ; i < n ; i++)
this->cstr[i] = src[i];
this->size=n;
}
charstring::charstring(const charstring& src)
{
this->size=src.size;
cstr=new char[this->size];
for(int i=0 ; i < size ; i++)
this->cstr[i] = src.cstr[i];
}
charstring::charstring(const charstring& src,int n)
{
this->size=src.size;
cstr=new char[this->size];
while(1)
{
if(n < this->size && n >= 0)
break;
cout<<"invalid input!!!\nsize to copy is greater than the contente size....enter again:"<<endl;
cin>>n;
}
for(int i=0 ; i < (this->size) ; i++)
this->cstr[i] = src.cstr[i];
this->size=n;
}
//operations
void charstring::copyfrom(const char* src)
{
int j=0 ;
for(; !(src[j]=='\0') ; j++);
if(j<=this->size)
{
int i=0;
for( ; !(src[i]=='\0') ; i++)
this->cstr[i]=src[i];
this->size=j;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const char* src ,int n, int start)
{
if(n<=this->size)
{
for( int i=start; (!(src[i]=='\0') || i < n ) ; i++)
this->cstr[i]=src[i];
this->size=n;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const charstring& src)
{
if(src.size< this->size)
{
for(int i=0 ; !(src.cstr[i]=='\0') ; i++)
this->cstr[i] = src.cstr[i];
this->size=src.size;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
void charstring::copyfrom(const charstring& src ,int n, int start)
{
if(n<this->size)
{
for( int i=start; (!(src.cstr[i]=='\0') || i < n) ; i++)
this->cstr[i] = src.cstr[i];
this->size=n;
}
else
{
cout<<"can`t copy!!! size is greater"<<endl;
}
}
//substrings
charstring charstring::substring(int n, int start )const
{
int i=start;
charstring str="0000000000000000000000000";
for( ; ( i < n) ; i++)
str.cstr[i] = this->cstr[i];
return str;
}
int charstring::findsubstring(const char* str , int start )const
{
int i=start;
for( ; !(this->cstr[i]=='\0') ; i++)
if(this->cstr[i]==str[i])
{
return i;
}
return -1;
}
int charstring::findsubstring(const charstring* str , int start )const
{
int i=start;
for( ; !(this->cstr[i]=='\0') ; i++)
if(this->cstr[i]==str->cstr[i])
{
return i;
}
return -1;
}
//concatenation operations
string& charstring::concat(const char* str)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for( int j=0; !(str[j] == '\0') ; j++,i++)
{
strt[i]=str[j];
}
return strt;
}
string& charstring::concat(const char* str ,int n , int start)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j=start ; (!(str[j] == '\0') || j < n) ; j++,i++)
{
strt[i]=str[j];
}
return strt;
}
string& charstring::append(const charstring& str)
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j= 0 ; (!(str.cstr[j] == '\0')) ; j++,i++)
{
strt[i]=str.cstr[j];
}
return strt;
}
string& charstring::append(const charstring& str , int n ,int start )
{
string strt;
int i=0;
for( ; !(this->cstr[i] == '\0') ; i++)
{
strt[i]=this->cstr[i];
}
for(int j= start ; (!(str.cstr[j] == '\0') || j < n) ; j++,i++)
{
strt[i]=str.cstr[j];
}
return strt;
}
//other operations
int charstring::getsize()const
{
int i=0;
for( ; !(cstr[i]=='\0') ; i++);
return i;
}
void charstring::reset()
{
cout<<"all contents are reset to \"0\""<<endl;
for(int i=0 ; !(cstr[i]=='\0') ; i++)
this->cstr[i]='0';
this->size=0;
}
void charstring::cutdown(int n)
{
if(n < this->size)
{
int i=n+1;
this->cstr[n]='\0';
for( ;i <= size ; i++)
this->cstr[i]=0;
this->size=n;
}
else
{
cout<<"invalid size!!!\nno change in string"<<endl;;
}
}
void charstring::print()const
{
cout<<"-----------------------------"<<endl;
cout<<"size is:"<<this->size<<endl;
cout<<"contente of the string are:"<<endl;;
for(int i=0 ; i<this->size ; i++)
cout<<this->cstr[i]<<" ";
cout<<endl;
cout<<"*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*"<<endl<<endl;
}
Comments
Post a Comment