string.h
#define MAXSTRLEN 255
#define ERROR 0
#define OK 1
typedef int Status;
typedef char String[MAXSTRLEN + 1];
//初始化字符串
Status StrAssign(String T, char e);
//有串S复制得串T
Status StrCopy(String T,String S);
//比较两个串的大小
Status StrCompare(String S,String T);
//获取串的长度
int StrLength(String S);
Status ClearString(String S);
Status ConCat(char *T,String S1,String S2);
Status SubString(String Sub,String S,int pos,int len);
Status Index(String S,String T,int pos);
Status Replace();
Status StrInsert();
Status StrDelete();
Status DestroyString(String S);
string.c
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void main() {
String S,S1,S2,Sub;
char a[1000],b[1000];
int c;
printf("请输入第一个字符串
");
gets_s(a,1000);
printf("请输入第二个字符串
");
gets_s(b,1000);
StrAssign(S,a);
StrAssign(S1,b);
//c = StrCompare(S,S1);//比较两个字符串
SubString(Sub,S,3,20);//字符串的截取
for (int i = 1; i <=Sub[0] ; i++)
{
printf("%c
",Sub[i]);
}
}
//获取一个字符串
Status StrAssign(String T,char a[]) {
int length=0;
for (int i = 0;a[i]!=' '; i++)
{
length++;
}
if (length > MAXSTRLEN) {
length = MAXSTRLEN;
}
T[0] = length;
for (int i = 0; i < length; i++)
{
T[i + 1] = a[i];
}
}
//字符串的比较
int StrCompare(String S, String T) {
int i = 1;
for ( i; (i<S[0])||(i<T[0]); i++)
{
if (S[i] != T[i]) {
return S[i] - T[i];
}
}
return 0;
}
//字符串的截取
Status SubString(String Sub, String S, int pos, int len) {
if (pos > S[0]|len<1|pos<1|len>S[0]-pos+1) {
return ERROR;
}
else {
Sub[0] = len;
int j = 1;
for (int i = pos; i <=pos+len; i++)
{
Sub[j] = S[i];
j++;
}
return OK;
}
}
//有字符串S得到字符串T
Status StrCopy(String T, String S) {
if (S[0]) {
return;
}
T[0] = S[0];
for (int j = 1; j <=S[0]; j++)
{
T[j] = S[j];
}
return OK;
}
//获取串的长度
int StrLength(String S) {
return S[0];
}
//将字符串拼接为字符串T
Status ConCat(String T, String S1, String S2) {
if (S1[0] + S2[0] > MAXSTRLEN) {
if (S1[0] > MAXSTRLEN) {
T[0] = MAXSTRLEN;
for (int i = 1; i <= MAXSTRLEN; i++) {
T[i] = S1[i];
}
}
else
{
T[0] = S1[0];
for (int i = 1; i <=S1[0]; i++) {
T[i] = S1[i];
}
int j = 1;
for (int j = 1; j <= MAXSTRLEN - S1[0];j++) {
T[T[0] + j] = S2[j];
}
T[0] = T[0] + j - 1;
}
}
else {
T[0] = S1[0] + S2[0];
int i;
for ( int i = 1; i <=S1[0]; i++)
{
T[i] = S1[0];
}
for (int j = 1; j <=S2[0];j++) {
T[i + j - 1] = S2[j];
}
}
return OK;
}