初学数据结构,求助实验C语言程序,请附带尽量详细说明,十分感激!

主要实验内容及要求:
实现串的基本操作.
例如:输入两个字符串s1,s2,联接为字符串s3,分别输出
s1,s2,s3等

#include<iostream.h>
#include <stdio.h>
#include <malloc.h>
#include<math.h>
#define M 100
typedef struct
{
char data[M];
int len;
}sqstring;
void Assign(sqstring &s,char t[])
{
int i=0;
while(t[i]!='\0')
{
s.data[i]=t[i];
i++;
}
s.len=i;
}
void Dispstr(sqstring s)
{
int i;
for(i=0;i<s.len;i++)
cout << s.data[i];
cout << endl;
}
int slength(sqstring s)
{
return (s.len);
}
strcopy(sqstring s,sqstring &t)
{
int i,j;
for(i=3,j=0;i<s.len;i++,j++)
t.data[j]=s.data[i];
t.len=j;
return(1);
}
sqstring concat(sqstring s,sqstring t)
{
sqstring r;
int i,j;
for(i=0;i<s.len;i++)
r.data[i]=s.data[i];
for(j=0;j<t.len;j++)
r.data[s.len+j]=t.data[j];
r.len=i+j;
return(r);

}
main()
{
cout <<"(1) 建立串 s,s1";
sqstring s,s1,s2,s3;
char t[]="abcdefghijklmn";
char d[]="xyz";
Assign(s,t);
Assign(s1,d);
cout << endl;
cout <<"(2) 输出串 s:";
Dispstr(s);
cout << endl;
cout <<"(3) 输出串 s 的长度:";
cout << slength(s);
cout << endl;
cout <<"(4) 输出串 s1:";
Dispstr(s1);
cout << endl;
cout <<"(5) 输出串 s1 的长度:";
cout << slength(s1);
cout << endl;
cout <<"(6) 提取串s中从第3个字符开始的4个字符而产生串s2:";
strcopy(s,s2);
cout << endl;
cout << endl;
cout <<"(7) 输出串 s2:";
Dispstr(s2);
cout <<"(8) 将串s1和串s2连接起来而产生串s3:";
s3=concat(s1,s2);
cout << endl;cout << endl;
cout <<"(9) 输出串 s3:";
Dispstr(s3);
cout << endl;
}
温馨提示:答案为网友推荐,仅供参考
相似回答