Home > Back-end >  String concatenation function by programming the strcat function, connect string t to the tail end o
String concatenation function by programming the strcat function, connect string t to the tail end o

Time:10-26

#include

Void strcen (char * s, char * t);

Void main ()
{
Char a []="abcde";
Char [b]="fghi";
Strcen (a, b);
Printf (" % s \ n ", a);
}

Void strcen (char * s, char * t)
{
int i,j;
For (I=0; S [I]!='\ 0'; I++)
;
For (j=0; T [j].='\ 0'; J++)
S [j] [I + j]=t;
S [I + j]='\ 0';
}



The following graphic error window appears after the program is running, trouble ask the great spirit, what was wrong with my application?

CodePudding user response:

1, the main function of a length of array without explicitly declared, so it is the length of the string length + 1 behind, when the child function called, string concatenation, a data is obviously subscript bounds,
2, the second for loop has logic errors,
3, the first for loop can need not, call strlen function directly,,
Void strcen (char * s, char * t);

Void main ()
{
Char a [50]="abcde";
Char [b]="fghi";
Strcen (a, b);
Printf (" % s \ n ", a);
}

Void strcen (char * s, char * t)
{
Int alen=strlen (s);
for(int i=0; T [I]!='\ 0'; I++) {
Alen +=I;
[I] a [alen]=t;
}
A [alen + 1]='\ 0';

}

  • Related