标签:
串(String)又称字符串,是一种特殊的线性表,表中的元素是单个字符,串是由n个字符组成的有限序列。
S="c1c2c3c4...cn"............(n>=0)
本文实现了串的初始化,串长度的计算,求子串,插入、删除、加法、找位置以及串的输出等函数。
String.h
#ifndef STRING_H_ #define STRING_H_ #include <iostream> class String { private: char *str; int size; public: String(); String(char *chx); ~String(); void Creat(); void Display(); int Length() const; String SubString(int pos, int num); void Insert(String s, int pos); void Delete(int pos, int num); String operator+(String s); int Find(String s, int pos); friend std::ostream &operator <<(std::ostream &os, String str); }; #endifStr.cpp
#include "StdAfx.h" #include "String.h" #include <iostream> using std::cout; using std::endl; using std::cin; String::String() { str=new char; if(!str) { cout<<"分配不成功"<<endl; } size=0; str[0]='\0'; } String::String(char *chx) { size=strlen(chx); str=new char[size]; strcpy(str,chx); } String::~String() { } void String::Creat() { char *ch; ch=new char; cout<<"Input string: "; cin>>ch; size=strlen(ch); str=new char[size+1]; strcpy(str,ch); } void String::Display() { cout<<"字符串为:"<<str<<endl; } int String::Length() const { return size; } //取其中一部分,从pos开始,去num个字符 String String::SubString(int pos, int num) { String tp; if(pos<0 || pos>size || num<=0) { cout<<"超出范围"<<endl; return tp; } int left=size-pos; if(num>left) { num=left; } delete []tp.str;//由于num的值可能改变,故对申明的字符串先释放再重新申明 tp.str=new char[num+1]; for(int i=0, j=pos-1; i<num; i++,j++) { tp.str[i]=str[j]; } tp.str[num]='\0'; tp.size=num; return tp; } void String::Insert(String s, int pos) { if(pos<0 || pos>size) { cout<<"超出范围"<<endl; } else { int size0=s.size+size; for(int i=size; i>=pos; --i) { str[s.size+i]=str[i]; } for(int i=pos; i<s.size+pos; i++) { str[i]=s.str[i-pos]; } size=size0; str[size]='\0'; } } void String::Delete(int pos, int num) { if(pos<0 || pos>size) { cout<<"超出范围"<<endl; } else { int left=size-pos; if(left<num) size=pos; else { for(int i=pos; i<size-num; i++) { str[i]=str[num+i]; } size=size-num; } str[size]='\0'; } } String String::operator+(String s) { strcat(str,s.str); size=size+s.size; str[size]='\0'; return *this; } //朴素模式匹配,该算法思路直观简明,但时间复杂度比较大。 int String::Find(String s, int pos) { int flag=0; --pos;//第pos字符位置在pos-1上 for(int i=pos,j=0; i<size; i++) { if(str[i]==s.str[j]) { for(int t=0; t<s.size; t++) { if(str[i+t]==s.str[t]) { ++flag; } } if(flag==s.size) { return i+1; } } flag=0; } return -1; } std::ostream &operator <<(std::ostream &os, String str) { cout<<str.str; return os; }string.cpp
// string.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "String.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { String str1; str1.Creat(); cout<<"字符串为:"<<str1<<endl; str1.Display(); cout<<"字符串的长度为:"<<str1.Length()<<endl; ///// String str(" is a good person!"); cout<<"字符串为:"<<str<<endl; str.Display(); cout<<"字符串的长度为:"<<str.Length()<<endl; ///// str=str1+str; cout<<"字符串为:"<<str<<endl; str.Display(); cout<<"字符串的长度为:"<<str.Length()<<endl; str.Insert("not ",13); cout<<"字符串为:"<<str<<endl; ///// String substr=str.SubString(13,10); cout<<"字符串为:"<<substr<<endl; ///// str.Delete(13,4); cout<<"字符串为:"<<str<<endl; char *findstr="person"; int pos=str.Find(findstr,1); cout<<"字符串\""<<findstr<<"\"的起始位置为:"; if(pos==-1) { cout<<"不存在!"<<endl; } else { cout<<pos<<endl; } system("pause"); return 0; }结果:
标签:
原文地址:http://blog.csdn.net/jiang111_111shan/article/details/46316895