#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *str;
int length;
}Hstring;
void insert(Hstring *s, Hstring *n, int pos)
{
int i;
char *temp;
temp = (char *)malloc(s->length + n->length);
for (i = 0; i<pos; i++)
temp = s->str;
for (i = 0; i<n->length; i++)
temp[pos + i] = n->str;
for (i = pos; i<s->length; i++)
temp[i + n->length] = s->str;
s->length = s->length + n->length;
s->str = temp;
}
void del(Hstring *s, int pos, int len)
{
int i;
char *temp;
temp = (char *)malloc(s->length - len);
for (i = 0; i<pos; i++)
temp = s->str;
for (i = pos; i<s->length - len; i++)
temp = s->str[i + len];
s->length = s->length - len;
s->str = temp;
}
int main()
{
int pos, len;
Hstring *s, *n;
s = n = (Hstring *)malloc(sizeof(Hstring));
printf("请输入字符串:");
scanf("%s", &s->str);
printf("请输入长度:");
scanf("%d", &s->length);
printf("请输入字符串:");
scanf("%s", &n->str);
printf("请输入长度:");
scanf("%d", &n->length);
printf("请输入插入位置:");
scanf("%d", &pos);
insert(&s, &n, pos);
printf("%s \n", &s->str);
printf("请输入要删除的位置:");
scanf("%d", &pos);
printf("请输入想删除的长度:");
scanf("%d", &len);
del(&s, &pos, len);
printf("%s \n", &s->str);
}
这样就行,不过好像什么都不会打印。 |