MaxSize = 100
class SqString():
def __init__(self,size=0):
self.data = [None]*MaxSize
self.size = 0
def StrAssign(self,str):
for i,item in enumerate(str):
self.data[i] = item
self.size += 1
def DispStr(self):
if self.size == 0:
raise ValueError("str is empty")
for i, item in enumerate(self.data):
if item is None:
break
else:
print(item)
def getsize(self,str):
print(self.size)
def Insstr(self,i,s1):
if i <= 0 or i > self.size:
raise ValueError("index out of range")
s2 = SqString(self.size + s1.size)
for k,item in enumerate(self.data[0:i]):
s2.data[k] = item
s2.size += 1
for k,item in enumerate(s1.data):
if item is not None:
s2.data[i+k] = item
s2.size += 1
else:
break
for k,item in enumerate(self.data[i:]):
if item is not None:
s2.data[i+s1.size+k] = item
s2.size += 1
else:
break
return s2
def Delstr(self,i,j):
if i <= 0 or i > self.size or i+j-1 > self.size:
raise ValueError("index out of range")
s2 = SqString()
for k,item in enumerate(self.data[0:i]):
s2.data[k] = item
s2.size += 1
for k,item in enumerate(self.data[i+j:]):
if item is not None:
s2.data[i+k] = item
s2.size += 1
else:
break
return s2
def Repstr(self,i,j,t):
if i <=0 or i > self.size or i+j-1 > self.size:
raise ValueError("Index out of range")
s2 = SqString(self.size-j + t.size)
for k,item in enumerate(self.data[0:i]):
s2.data[k] = item
s2.size += 1
for k,item in enumerate(t.data):
if item is not None:
s2.data[i-1+k] = item
s2.size += 1
for k,item in enumerate(self.data[i+j-1:]):
if item is not None:
s2.data[i-1+t.size+k] = item
s2.size += 1
return s2
def getstr(self,i,j):
if i <= 0 or i > self.size or i+j > self.size:
raise ValueError("index out of range")
s3 = SqString(self.size)
for k,item in enumerate(self.data[i:i+j]):
if item is not None:
s3.data[k] = item
s3.size += 1
else:
break
return s3
def Concat(self,s2):
s4 = SqString(self.size+s2.size)
for k,item in enumerate(self.data):
if item is not None:
s4.data[k] = item
s4.size += 1
else:
break
for i,item in enumerate(s2.data):
if item is not None:
s4.data[k+i] = item
s4.size += 1
else:
break
return s4
if __name__=='__main__':
s_1 = "abcdefghefghijklmn"
s1 = SqString()
s1.StrAssign(s_1)
s_2 = "xyz"
s2 = SqString()
s2.StrAssign(s_2)
a = s1.Concat(s2)
a.DispStr()
|