#include <stdio.h>
#include "sqString.h"
int main()
{
SqString s;
printf("实现将字符串S中所有值为c1的字符换成值为c2的字符:");
StrAssign(s, "wangxu");
Trans(s, 'e', 'a');
DispStr(s);
SqString s1;
printf("实现将字符串反序:");
StrAssign(s1, "abcdefg");
Invert(s1);
DispStr(s1);
printf("从串s中删除其值等于g的所有字符:");
StrAssign(s, "wangxu");
DellChar(s, 'g');
DispStr(s);
printf("求公共字符:");
SqString s2;
StrAssign(s1, "wangxuqijq");
StrAssign(s2, "ddgpwangxu");
s = CommChar(s1, s2);
DispStr(s);
return 0;
}
SqString CommChar(SqString s1,SqString s2)
{
SqString s3;
int i,j,k=0;
for (i=0; i<s1.length; i++)
{
for (j=0; j<s2.length; j++)
if (s2.data[j]==s1.data[i])
break;
if (j<s2.length) //s1.data[i]是公共字符
{
s3.data[k]=s1.data[i];
k++;
}
}
s3.length=k;
return s3;
}
void DellChar(SqString &s, char c)
{
int k=0, i=0; //k记录值等于c的字符个数
while(i<s.length)
{
if(s.data[i]==c)
k++;
else
s.data[i-k]=s.data[i];
i++;
}
s.length -= k;
}
void StrAssign(SqString &s,char cstr[]) //字符串常量cstr赋给串s
{
int i;
for (i=0;cstr[i]!='\0';i++) //字符串常量cstr元素为空时终止
s.data[i]=cstr[i]; //赋值
s.length=i; //改变s的长度
}
void Trans(SqString &s, char c1, char c2)
{
int i;
for (i=0; i<s.length; i++)
if (s.data[i]==c1)
s.data[i]=c2;
}
void DispStr(SqString s)//输出串
{ int i;
if (s.length>0)
{
for (i=0;i<s.length;i++)
printf("%c",s.data[i]);
printf("\n");
}
}
void Invert(SqString &s)
{
int i;
char temp;
for (i=0; i<s.length/2; i++)
{
temp = s.data[i];
s.data[i]=s.data[s.length-i-1];
s.data[s.length-i-1] = temp;
}
}
#ifndef SqString_H_INCLUDED
#define SqString_H_INCLUDED
#define MaxSize 100 //最多的字符个数
typedef struct
{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间
int length; //标记当前实际串长
} SqString;
void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串s
void StrCopy(SqString &s,SqString t); //串t复制给串s
bool StrEqual(SqString s,SqString t); //判串相等
int StrLength(SqString s); //求串长
SqString Concat(SqString s,SqString t); //串连接
SqString SubStr(SqString s,int i,int j); //求子串
SqString InsStr(SqString s1,int i,SqString s2); //串插入
SqString DelStr(SqString s,int i,int j) ; //串删去
SqString RepStr(SqString s,int i,int j,SqString t); //串替换
void DispStr(SqString s); //输出串
void Trans(SqString &s, char c1, char c2);
void Invert(SqString &s);
void DellChar(SqString &s, char c);//从串s中删除其值等于c的所有字符
SqString CommChar(SqString s1,SqString s2);//求公共字符
#endif // LISTRING_H_INCLUDED