/*
编写一个函数void replace(char *str1,char *str2,int i,int j),将字符串中str1中的第i个字符开始到j个字符结束的位置替换为str2.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int strLen(char *str)
{
char *p = NULL;
p = str;
int strLen = 0;
while(*p != '\0')
{
strLen++;
p++;
}
return strLen;
}
int replace(char *str1,char *str2,int i,int j)
{
int distance = j - i + 1;//要替代的字符数
int str2Len = strLen(str2);
if(distance != str2Len)
return 0;
int currentIndex = i - 1;
int endIndex = j - 1;
char *p = str2;
while(*p != '\0' && currentIndex <= endIndex)
{
*(str1 + currentIndex) = *p;
++currentIndex;
++p;
}
return 1;
}
int main()
{
char str1[10] = "abcdefghi";
char str2[10] = "123";
int start,end;
scanf("%d%d", &start, &end);
replace(str1,str2,start,end);
printf("%s", str1);
return 0;
}