C++string类使用及定义
来源:优易学  2011-12-11 10:23:17   【优易学:中国教育考试门户网】   资料下载   IT书店

  #define strmax 100
  #include <iostream.h>
  #include <cstring>
  class string
  {
  char line[100];
  int x,y;
  public:
  string(char *);
  void print();
  void turn();
  int strlength();
  void substring(unsigned,unsigned);
  void insert(int,char *);
  void index(char *);
  void replace(char *,char *);
  void strempty();
  string operator (string s);
  string operator + (string s);
  void operator < (string s1);//判断字符窜长度
  void operator (string s1);//字符窜相等
  void operator ! (string s1);//字符窜不相等
  char operator [] (int pos);
  };
  string::string(char *n)
  {
  strcpy(line,n);
  }
  void string::print()
  {
  cout<< line;
  }
  void string::turn()
  {
  int i,lenstrlen(line);
  for(i0;i<len;i++)
  {
  if(line[i]>97&&line[i]<122)
  {
  line[i]-32;
  cout<< line[i];
  }
  else
  {
  cout<< line[i];
  }
  }
  }
  int string::strlength()
  {
  return strlen(line);
  }
  void string::substring(unsigned pos,unsigned len)
  {
  if(pos > strlen(line))
  cout<<"Error!";
  else
  {
  if((strlen(line)-pos) < len)
  lenstrlen(line)-pos;
  cout<<"The substring is:";
  unsigned ipos+len;
  for(pos;pos<i;pos++)
  {
  cout<< line[pos-1];
  }
  }
  }
  void string::insert(int pos,char *p)
  {
  char p1[64];
  char p2[64];
  strcpy(p1,p);
  cout<<"The line which will be insert is:"<<p1;
  int lstrlen (p1);
  int m0;
  int c0;
  for(int bpos-1;b<64;b++,c++)
  {
  p2[c]line[b];
  }
  for(int apos-1;a<pos+l;a++,m++)
  {
  line[a]p1[m];
  }
  strcat(line,p2);
  cout<<endl<<"The line after inserting is:"<<line;
  }
  void string::index(char *n)
  {
  char *p;
  char n1[100];
  char n2[100];
  strcpy(n1,line);
  strcpy(n2,n);
  pstrstr(n1,n2);
  int l;
  lstrlen(n1);
  cout<<"The words you want to find is:"<<n;
  if(p)
  {
  strcpy(n2,p);
  for(int i0;i<l;i++)
  {
  if(n1[i]n2[0])
  {
  cout<<endl<<"The position of the words which you wanted is "<<i+1;
  break;
  }
  }
  }
  else
  {
  cout<<endl<<"There isn’t the words which you wanted.";
  }
  }
  void string::replace(char *q,char *p)//q被替换串,青年人网提示p替换串
  {
  char p1[100];
  char p2[100];
  char p3[100];
  strcpy(p1,line);
  strcpy(p2,p);
  strcpy(p3,q);
  pstrstr(p1,p3);
  if(p)
  {
  int i,j;
  int lpstrlen(p);
  int lp1strlen(p1);
  int lp2strlen(p2);
  int lp3strlen(p3);
  cout<<"The sourse string is : "<<line<<endl<<"You want to change : "<<p3;
  for(ilp1-lp,j0;j<lp2;i++,j++)
  line[i]p2[j];
  for(jlp3;p[j];i++,j++)
  line[i]p[j];
  line[i]’\0’;
  cout<<endl<<"After change : "<<line;
  }
  else
  {
  cout<<q<<" is not the substring of "<<line;
  }
  }

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训