C Tutorial/string.h/strncat — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Текущая версия на 10:32, 25 мая 2010
strncat
Item Value Header file string.h Declaration char *strncat(char *str1, const char *str2, size_t count); Function concatenates not more than count characters of *str2 to *str1 and terminates str1 with a null. Return returns *str1.
You have to ensure that str1 is large enough to hold both its str1 and str2.
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[80];
unsigned int len;
gets(s1);
gets(s2);
len = 79-strlen(s2);
strncat(s2, s1, len);
printf(s2);
return 0;
}
Using strcat and strncat
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ 20 ] = "Happy ";
char s2[] = "New Year ";
char s3[ 40 ] = "";
printf( "s1 = %s\ns2 = %s\n", s1, s2 );
printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) );
printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) );
printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) );
return 0;
}
s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year