what is wrong with this code. the problem is flagged as /*here*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stringsize1 256 /*length of original*/
#define stringsize2 80 /*length of second string*/
#define numsize 10
void insert(char original[], char sub[],int pos); /*function*/
main()
{
char str1[stringsize1], str2[stringsize2],num[numsize];
int position;
int i;
for (i=0;i<5;i++)
{
printf("\nPlease enter a character string:\n");
gets(str1);
printf("\nPlease enter the inserting character string:\n");
gets(str2);
printf("\nPlease enter the inserting position:\n");
gets(num);
/*HERE*/position=pos(num);
insert(str1,str2,position);
printf("\nThe new string is:\n");
printf("%s\n",str1);
}
return EXIT_SUCCESS;
}
/*The inserting function body*/
void insert(char origin[], char sub[],int pos)
{
int i, j; /*i=original string,j=substring*/
int firstlength;
int seclength;
char temp[stringsize1]; /*buffer*/
firstlength=strlen( origin);
seclength=strlen( sub);
/*copy the original str to position in buffer*/
for (i=0;i<pos;++i)
temp[i]=origin[i];
/*insert substr to buffer at desired location*/
for (j=0;j<seclength;j++,i++)
temp[i]=sub[j];
/*remainder of original string copied to buffer*/
/*here*/ for (pos<firstlength;pos++;i++)
temp[i]=origin[pos];
/*end the string*/
temp[i]='\0';
/*complete string copied back to the original location*/
strcpy(origin,temp);
}



LinkBack URL
About LinkBacks


