Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Concatenating Strings

C Language Examples - Concatenating Strings

C Language Examples C Language Examples

Concatenate two strings using strcat().

Example

#include

<

stdio.h

int

main

(

)

{

char

s1

[

100

]

,

s2

[

100

]

,

i

,

j

;

printf

(

"

Enter the first string:

"

)

;

scanf

(

"

%s

"

,

s1

)

;

printf

(

"

Enter the second string:

"

)

;

scanf

(

"

%s

"

,

s2

)

;

// Calculate the length of string s1

for

(

i

=

0

;

s1

[

i

]

!= '\

0

'; ++

i

)

;

for

(

j

=

0

;

s2

[

j

]

!= '\

0

'; ++

j

, ++

i

)

{

s1

[

i

]

=

s2

[

j

]

;

}

s1

[

i

]

= '\

0

';

printf

(

"

After concatenation: %s

"

,

s1

)

;

return

0

;

}

The output is:



输入第一个字符串: google

输入第二个字符串: runoob

连接后: googlerunoob

C Language Examples C Language Examples