程序目的是为将串2插入串1某一指定位置中
不知道为什么插入串2后输出会出现乱码,希望有人能教一下要怎么修改
插入函数在35行
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
#define false 0
#define true 1
typedef struct str
{
char data[MaxSize];
int length;
}SqString;
void InitSqString(SqString* str1, SqString* str2)
{
str1->length = str2->length = 0;
}
void StrAssign(SqString* str1, SqString* str2,char *a,char *b)
{
int i=0;
while (a[i]!='\0')
{
str1->length++;
str1->data[str1->length - 1] = a[i];
i++;
}
i = 0;
while (b[i] != '\0')
{
str2->length++;
str2->data[str2->length - 1] = b[i];
i++;
}
}
int InsertStr(SqString* str1, SqString* str2, int post)
{
if (post<0 || post-1>str1->length) {
printf("error\n");
return false;
}
int i;
for (i = post - 1; i < str1->length; i++)
{
str1->data[post + str2->length] = str1->data[i];
}
for (i = 0; i < str2->length; i++)
{
str1->data[post - 1 + i] = str2->data[i];
}
str1->length += str2->length;
return true;
}
void printStr(SqString *str1)
{
int i;
for (i = 0; i < str1->length; i++)
{
printf("%c", str1->data[i]);
}
}
int main()
{
SqString str1,str2;
char a[10] = "abcde", b[10] = "fghij";
int post;
InitSqString(&str1, &str2);
StrAssign(&str1, &str2, a, b);
printf("please enetr postion of you want to insert\n");
scanf_s("%d", &post);
InsertStr(&str1, &str2, post);
printStr(&str1);
}