字符串类HString实现

HString.h
 1  #ifndef HSTRING_H
 2  #define  HSTRING_H
 3 
 4  #include  < iostream >
 5  using   namespace  std;
 6 
 7  const   int  MAX_SIZE  =   128 ;
 8 
 9  class  HString
10  {
11  public :
12      HString();
13      HString( const  HString  & str);
14      HString( const   char   * str);
15       ~ HString(){ delete [] ch; }
16 
17       bool  empty()  const return  len  ==   0   ?   true  :  false ; }
18       int  length()  const  {  return  len; }
19       void  strClear(HString  & str){ len  =   0 ; ch[ 0 =   ' \0 ' ; }
20       char   * toArray() {  return  ch; }
21 
22       char   & operator []( int  i)  const ;
23       bool   operator ! (){  return  empty(); }
24      HString  & operator = ( const  HString  & str);
25      HString  & operator + ( const  HString  & str);
26      HString  & operator += ( const  HString  & str);
27 
28      HString  & copy( const  HString  & str);
29       int  find(HString  & pat);
30       int  find( char   * pa);
31 
32       void  print();
33 
34 
35 
36  private :
37       char   * ch;
38       int  len;
39 
40  };
41 
42  #endif

HString.cpp
  1  #include  " HString.h "
  2 
  3  HString::HString()
  4  {
  5      ch  =   new   char [MAX_SIZE  +   1 ]; // tail is '\0'
  6       if (ch  ==  NULL)
  7      {
  8          cerr  <<   " Allocation failed! \n " ;
  9          exit( - 1 );
 10      }
 11 
 12      len  =   0 ;
 13      ch[ 0 =   ' \0 ' ;
 14  }
 15 
 16  HString::HString( const  HString  & str)
 17  {
 18      ch  =   new   char [MAX_SIZE  +   1 ];
 19       if (ch  ==  NULL)
 20      {
 21          cerr  <<   " Allocation failed! \n " ;
 22          exit( - 1 );
 23      }
 24 
 25       int  i;
 26      len  =  str.length();
 27       for (i  =   0 ; i  <  len; i ++ )
 28      {
 29          ch[i]  =  str[i];
 30      }
 31      ch[len]  =   ' \0 ' ;
 32  }
 33 
 34  HString::HString( const   char   * str)
 35  {
 36      ch  =   new   char [MAX_SIZE  +   1 ];
 37       if (ch  ==  NULL)
 38      {
 39          cerr  <<   " Allocation failed! \n " ;
 40          exit( - 1 );
 41      }
 42      
 43       int  i  =   0 ;
 44       while (str[i]  !=   ' \0 ' )
 45      {
 46          i ++ ;
 47      }
 48 
 49 
 50      len  =  i;
 51 
 52       for (i  =   0 ; i  <  len; i ++ )
 53      {
 54          ch[i]  =  str[i];
 55      }
 56      ch[len]  =   ' \0 ' ;
 57  }
 58 
 59  void  HString::print()
 60  {
 61      cout  <<  len  <<   "  elements:  " ;
 62       for ( int  i  =   0 ; i  <  len; i ++ )
 63      {
 64          cout  <<  ch[i]  <<   "   " ;
 65      }
 66      
 67      cout  <<  endl;
 68  }
 69 
 70 
 71  char   & HString:: operator []( int  i)  const
 72  {
 73       if (i  <   0   ||  i  >  len)
 74      {
 75          cerr  <<   " Invailed index! \n " ;
 76          exit( - 1 );
 77      }
 78 
 79       return  ch[i];
 80  }
 81 
 82  int  HString::find(HString  & pat)
 83  {
 84       char   * pa  =  pat.toArray();
 85      
 86       if ( * pa  ==   ' \0 '   ||  empty())
 87      {
 88          cerr  <<   " Src or pattern string NULL \n " ;
 89          exit( - 1 );
 90      }
 91 
 92       int  i  =   0 , j  =   0 ;
 93      
 94       while (i  <  len  &&  j  <  pat.length())
 95      {
 96           if (ch[i]  ==  pa[j])
 97          {
 98              i ++ ;
 99              j ++ ;
100          }
101           else
102          {
103              i  =  i  -  j  +   1 ;
104              j  =   0 ;
105          }
106           // cout << i << ": " << ch[i] << " " << j << " " << pa[j] << endl;
107 
108      }
109 
110       if (j  >=  pat.length())
111           return  i  -  j;
112       else
113           return   - 1 ;
114  }
115 
116 
117  int  HString::find( char   * pa)
118  {
119      HString pat(pa);
120       return  find(pat);
121      
122  }
123 
124  HString  & HString:: operator = ( const  HString  & str)
125  {
126       return   this -> copy(str);
127  }
128 
129 
130  HString  & HString:: operator += ( const  HString  & str)
131  {
132       if (str.empty())
133           return   * this ;
134      
135       if (len  +  str.length()  >  MAX_SIZE)
136      {
137          cerr  <<   " cancat string too long \n " ;
138          exit( - 1 );
139      }
140 
141       for ( int  i  =   0 ; i  <  str.length(); i ++ )
142      {
143          ch[len  +  i]  =  str[i];
144      }
145      ch[len  +  str.length()]  =   ' \0 ' ;
146 
147      len  =  len  +  str.length();
148 
149       return   * this ;
150  }
151 
152  HString  & HString:: operator + ( const  HString  & str)
153  {
154      HString  * newStr  =   new  HString( * this );
155      ( * newStr)  +=  str;
156 
157       return   * newStr;
158  }
159 
160  HString  & HString::copy( const  HString  & str)
161  {
162       if ( & str  ==   this )
163           return   * this ;
164 
165      delete [] ch;
166      ch  =   new   char [MAX_SIZE  +   1 ];
167       if (ch  ==  NULL)
168      {
169          cerr  <<   " Allocation failed \n " ;
170          exit( - 1 );
171      }
172 
173      len  =  str.length();
174       for ( int  i  =   0 ; i  <  len; i ++ )
175      {
176          ch[i]  =  str[i];
177      }
178 
179       return   * this ;
180  }
181 
182 

main.cpp
 1  #include  < iostream >
 2  #include  " HString.h "
 3  using   namespace  std;
 4 
 5  int  main()
 6  {
 7      HString str( " abcdefe " );
 8      str.print();
 9 
10      HString pat( " de " );
11 
12      pat.print();
13      cout  <<  str.find(pat)  <<  endl;
14      cout  <<  str.find( " cd " <<  endl;
15 
16  //     str = pat;
17  //     str.print();
18 
19      str  +=  pat;
20      str.print();
21 
22      HString ns;
23      ns  =  str  +  pat;
24      ns.print();
25 
26 
27       return   0 ;
28 
29  }

转载于:https://www.cnblogs.com/chio/archive/2007/06/10/777893.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下载好代码后直接在linux环境下减压,make之后即可产生可执行代码,压缩文件中已经包含了可执行代码。 通过串的堆分配存储结构来实现串的以下功能: //生成一个值等于串常量chars的串string int StrAssign(HString *str,char *chars); //返回串string的长度 int StrLength(HString str); //比较两个串的大小,如果str1 > str2,返回值>0,如果相等,返回0,如果str1 < str2,返回值<0 int StrCompare(HString str1,HString str2); //清空串,释放串所占用的空间 int ClearString(HString *str); //返回串Str1和Str2联合而成的串 HString Concat(HString str1,HString str2); //返回串str的第pos个字符之后的长度为len的子串 HString SubString(HString str,int pos,int len); //显示字符串 int StrTrave(HString str); //-----------------------附加串操作函数-------------------------- //以下串操作可由基本串操作来实现 //串str1复制得到str2 int StrCopy(HString str1,HString *str2); //串str为空串,返回1,否则返回0 int StrEmpty(HString str); //如果主串str中存在和串substr相等的子串,则返回子串在主串中pos个字符之后第一次出现的位置 ,运用了KMP算法 int Index(HString str,HString substr,int pos); //Index中包括了一个静态函数get_next(),这个函数可以得到字符串的最简匹配值(kmp算法中字符匹配失败后的下一个最佳匹配值) //用字符串Tstr替换主串str中出现的所有与substr相等的子串 int StrReplace(HString **str,HString substr,HString Tstr); //在串str的第pos个字符之后插入串substr int StrInsert(HString *str,HString substr,int pos); //从串str的第pos个字符起删除len个字符 int StrDelete(HString **str,int pos,int len); //销毁现有串str int StrDestory(HString *str);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值