/*
串的定长顺序存储
author: kk.h
*/
#include "stdio.h"
#include "conio.h"
#include "string.h"
#define MaxStrLen 200
/* 字符串赋值,把串source复制到target中 */
int StrAssign(char* target,char *source)
{
int i=0;
for(target[0]=source[0];source[i]!='\0';i++)
target[i]=source[i];
target[i]=source[i];
return 1;
}
/* 字符串连接,把串s1和s2连接成t */
int Concat(char*t,char*s1,char*s2)
{
int i=0,j=0;
for(t[0]=s1[0];s1[i]!='\0';i++)
t[i]=s1[i];
for(;s2[j]!='\0';i++,j++)
t[i]=s2[j];
t[i]='\0';
return 1;
}
/* 求子串,将串s中从第pos个字符开始长度为len的字符序列复制到串sub中 */
int SubString(char*sub,char*s,int pos,int len)
{
int i;
for(i=0;i<len;i++)
sub[i] = s[pos-1+i];
sub[len]='\0';
return 1;
}
//返回串的长度
int StrLength(char*s1)
{
return strlen(s1);
}
//比较s1和s2的大小 比较两个串的第一个字符的ASCII码
int strcmp(char*s1,char*s2)
{
int i;
for(i=0;i<strlen(s1)&&i<strlen(s2);++i)
if(s1[i]!=s2[i])return s1[i]-s2[i];
return strlen(s1) -strlen(s2);
}
//在串s的第pos个字符之前插入串t。直接实现或者利用strlength(),SubString()和Concat()。
int strinsert(char*s,int pos,char *t)
{
SubString(s,t,pos,StrLength(t));
printf("\n%s",s);
return 1;
}
main()
{
char s1[MaxStrLen],s2[MaxStrLen],t[MaxStrLen],t1[MaxStrLen],t2[MaxStrLen];
StrAssign(s1,"i love you ");
StrAssign(s2,",that is impossible!");
Concat(t,s1,s2);
printf("字符串s1是:%s\n",s1);
printf("字符串s2是:%s\n",s2);
printf("\n合并后的字符串:%s",t);
SubString(t1,t,1,20);
SubString(t2,t,23,9);
printf("\n子串:%s%s\n",t1,t2);
printf("\n串s1的长度是;%d\n",StrLength(s1));//
int result;
result = strcmp(s1,s2);
if(result>0)
printf("\ns1大于s2\n");
if(result<0)
printf("\ns1小于s2\n");
if(result = 0)
printf("\ns1等于s2\n");
strinsert(t,2,s1);
getch();
}