Home > Back-end >  C beginners ask - string concatenation
C beginners ask - string concatenation

Time:01-27

 int main () 
{////* * * a connection string
Char str1 [50], str2 [20].
int i,j;
Printf (" input the first string (including Spaces) : \ n ");
The scanf (" % s ", str1);
Printf (" input the second string (including Spaces) : \ n ");
The scanf (" % s ", str2);
I=j=0;
While (str1 [I]!='\ 0') {I=I + 1; }
While (str2 [j].='\ 0')
{
Str1=[I + 1] str2 [j];
Printf (" connection string 1: % s \ n ", str1);
I=I + 1;
J=j + 1;
}
//strcat (str1, str2);
Printf (" \ n connection string 1: % s \ n ", str1);
return 0;
is there anyone who help me to solve, I this period of c program is what can't achieve the connection string?

CodePudding user response:

 # include & lt; stdio.h> 

# define the DEBUG

Int main ()
{////* * * a connection string
Char str1 [50], str2 [20].
int i,j;
Printf (" input the first string (including Spaces) : \ n ");
The scanf (" % s ", str1);
Printf (" input the second string (including Spaces) : \ n ");
The scanf (" % s ", str2);
I=j=0;
While (str1 [I]!='\ 0') {I=I + 1; }
While (str2 [j].='\ 0')
{
//str1=[I + 1] str2 [j];
Str1=str2 [I] [j];
# ifdef DEBUG
Str1 [I + 1)=0;
Printf (" connection string 1: % s \ n ", str1);
# endif
I=I + 1;
J=j + 1;
}
Str1 [I]=0;//equal '\ 0'
//strcat (str1, str2);
Printf (" \ n connection string 1: % s \ n ", str1);
return 0;
}


For your reference ~

At the end of the first string, that is, '\ 0' position for the second string of the first character, rather than the first string '\ 0' in the next place to store the second the first character of string

In addition, the string is a '\ 0' end, so to % s output, you need to add '\ 0'

CodePudding user response:

Upstairs said, and array initialization time to reset, the operation not to overflow
  • Related