如果串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("请输入第一个字符串:
");
gets(ch);
StrAssign(&S1,ch);
printf("输出串S1:");
StrPrint(S1);
printf("请输入第二个字符串:
");
gets(ch);
StrAssign(&S2,ch);
printf("输出串S2:");
StrPrint(S2);
printf("将串S2插入到S1的第13个位置:
");
StrInsert(&S1,13,S2);
StrPrint(S1);
printf("将串S1中的第22个位置起的7个字符删除:
");
StrDelete(&S1,22,7);
StrPrint(S1);
printf("将串S2中的第6个位置起的4个字符取出放进Sub中:
");
SubString(&Sub,S2,6,4);
StrPrint(Sub);
printf("将串Sub赋值为America:
");
StrAssign(&Sub,"America");
printf("将串S1中的串S2用Sub代替:
");
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]!='