您的位置:首页 > 其它

串的应用举例

2015-07-26 08:39 405 查看
假设串S1 = "I come from Beijing",S2 = "Chongqing" ,Sub = "America".

利用串的基本操作,如果串的赋值、串的插入、串的删除、串的替换、对上面

的串进行操作
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 60
typedef struct
{
char str[MAXSIZE];
int length;
}SeqString;

void StrAssign(SeqString *S,char cstr[]);//串的赋值操作
int StrEmpty(SeqString S);//判断串是否为空
int StrLength(SeqString S);//求串的长度操作
void StrCopy(SeqString *T,SeqString S);//串的复制操作
int StrCompare(SeqString S,SeqString T);//串的比较操作
int StrInsert(SeqString *S,int pos,SeqString T);//串的插入操作
int StrDelete(SeqString *S,int pos,int len);//串的删除操作
int StrConcat(SeqString *T,SeqString S);//串的连接操作
int SubString(SeqString *Sub,SeqString S,int poos,int len);//截取子串操作
int StrReplace(SeqString *S,SeqString T,SeqString V);//串的替换操作
int StrIndex(SeqString S,int pos,SeqString T);//串的定位操作
void StrClear(SeqString *S);//清空串操作
void StrPrint(SeqString S);//串的输出声明

#include "string.h"

int main(void)
{
SeqString S1,S2,Sub;
char ch[MAXSIZE];
printf("请输入第一个字符串:\n");
gets(ch);
StrAssign(&S1,ch);
printf("输出串S1:");
StrPrint(S1);
printf("请输入第二个字符串:\n");
gets(ch);
StrAssign(&S2,ch);
printf("输出串S2:");
StrPrint(S2);
printf("将串S2插入到S1的第13个位置:\n");
StrInsert(&S1,13,S2);
StrPrint(S1);
printf("将串S1中的第22个位置起的7个字符删除:\n");
StrDelete(&S1,22,7);
StrPrint(S1);
printf("将串S2中的第6个位置起的4个字符取出放进Sub中:\n");
SubString(&Sub,S2,6,4);
StrPrint(Sub);
printf("将串Sub赋值为America:\n");
StrAssign(&Sub,"America");
printf("将串S1中的串S2用Sub取代:\n");
StrReplace(&S1,S2,Sub);
StrPrint(S1);
return 0;
}

#include "string.h"

void StrAssign(SeqString *S,char cstr[])//串的赋值操作(将常量cstr中的字符赋值给串S)
{
int i = 0;
for(i = 0;cstr[i]!='\0';i++)
{
S->str[i] = cstr[i];
}
S->length = i;
}
int StrEmpty(SeqString S)//判断串是否为空
{
if(S.length == 0)
{
return 1;
}
else
{
return 0;
}
}
int StrLength(SeqString S)//求串的长度操作
{
return S.length ;
}
void StrCopy(SeqString *T,SeqString S)//串的复制操作(将串S中的每一个字符赋给T)
{
int i;
for(i = 0;i < S.length ;i++)
{
T->str[i] = S.str[i];
}
T->length = S.length ;
}
int StrCompare(SeqString S,SeqString T)//串的比较操作
{
int i;
for(i = 0;i < S.length&&i < T.length ;i++)//比较两个串中的字符
{
if(S.str[i] != T.str[i])//如果出现字符不同,则返回两个字符的差值
{
return (S.str[i]-T.str[i]);
}
}
return (S.length - T.length);//如果比较完毕,返回两个字符串的长度的差值
}
int StrInsert(SeqString *S,int pos,SeqString T)//串的插入操作(在串S的pos个位置插入串T)
{
int i;
if(pos < 0 || pos-1 > S->length)
{
printf("插入位置不正确\n");
return 0;
}
if(S->length + T.length <= MAXSIZE)//子串完整插入到串中
{
//在插入子串T前,将S中的pos后的字符向后移动len个位置
for(i = S->length+T.length-1;i >= pos+T.length-1;i--)
{
S->str[i] = S->str[i-T.length];
}
//将串插入到S中
for(i = 0;i < T.length ;i++)
{
S->str[pos+i-1] = T.str[i];
}
S->length = S->length +T.length ;
return 1;
}
else if(pos +T.length <= MAXSIZE)//子串完全插入S中,但是S中的字符会被截断
{
for(i = MAXSIZE-1;i > T.length +pos-1;i--)
{
S->str[i] = S->str[i-T.length];
}
for(i = 0;i < T.length ;i++)
{
S->str[i+pos-1] = T.str[i];
}
S->length = MAXSIZE;
return 0;
}
else//子串T不能完全插入到S中,T将会有字符被舍弃
{
for(i = 0;i < MAXSIZE-pos;i++)
{
S->str[i+pos-1] = T.str[i];
}
S->length = MAXSIZE;
return 0;
}
}
int StrDelete(SeqString *S,int pos,int len)//串的删除操作(在串S中删除pos开始的len个字符,然后将后面的字符向前移动)
{
int i,flag;
if(pos < 0 || len < 0 || pos+len-1 > S->length)
{
printf("删除位置不正确,参数len不合法\n");
flag = 0;
}
else
{
for(i = pos+len;i <= S->length-1;i++)
{
S->str[i-len] = S->str[i];
}
S->length = S->length -len;//修改串S的长度
flag = 1;
}
return flag;
}
int StrConcat(SeqString *T,SeqString S)//串的连接操作(将串S连接在串T的后面)
{
int i,flag;
if(T->length +S.length <= MAXSIZE)
{
for(i = T->length ;i < T->length +S.length ;i++)
{
T->str[i] = S.str[i-T->length];
}
T->length = T->length +S.length ;
flag = 1;
}
else if(T->length < MAXSIZE)
{
for(i = T->length ;i < MAXSIZE;i++)
{
T->str[i] = S.str[i-T->length];
}
T->length = MAXSIZE;
flag = 0;
}
return flag;
}
int SubString(SeqString *Sub,SeqString S,int pos,int len)//截取子串操作(截取串S中从第pos个字符开始,长度为len的连续字符,并赋值给Sub)
{
int i;
if(pos < 0 || len < 0 || pos+len-1 > S.length)
{
printf("参数pos和len不合法\n");
return 0;
}
else
{
for(i = 0;i < len;i++)
{
Sub->str[i] = S.str[i+pos-1];
}
Sub->length = len;
return 1;
}
}
int StrIndex(SeqString S,int pos,SeqString T)//串的定位操作(在主串S中的第pos个位置开始查找子串T,如果主串S中存在与串T值相等的子串,返回子串在主串第pos个字符后第一次出现的位置)
{
int i,j;
if(StrEmpty(T))
{
return 0;
}
i = pos;
j = 0;
while(i < S.length && j < T.length)
{
if(S.str[i] == T.str[j])
{
i++;
j++;
}
else//如果当前对应位置的字符不相等,则从串S的下一个字符开始,从T的第0个字符开始比较
{
i = i-j+1;
j = 0;
}
}
if(j >= T.length)//如果在串S中找到串T,则返回子串T在主串S中的位置
{
return i-j+1;
}
else
{
return -1;
}
}
int StrReplace(SeqString *S,SeqString T,SeqString V)//串的替换操作(如果串S中存在子串T,则用V替换串S中的所有子串T)
{
int flag;
int i = 0;
if(StrEmpty(T))
{
return 0;
}
while(i)
{
i = StrIndex(*S,i,T);//利用串的定位操作在串S中查找T的位置
if(i)
{
StrDelete(S,i,StrLength(T));//如果找到子串T,则将S中的串T删除
flag = StrInsert(S,i,V);//将V插入
if(!flag)
{
return 0;
}
i += StrLength(V);
}
}
return 1;
}
void StrClear(SeqString *S)//清空串操作
{
S->length = 0;
}
void StrPrint(SeqString S)//串的输出声明
{
int i;
for(i = 0;i < S.length ;i++)
{
printf("%c",S.str[i]);
}
printf("\n");
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: