Loading, please wait...

String-Manipulation Functions

The string-handling library (<string.h>) provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and other strings, tokenizing strings (separating strings into logical pieces) and determining the length of strings. This section presents the string-manipulation functions of the string-handling library.

 

The functions are summarized in the following table.

 

Function prototype

Function Description

char *strcpy( char *s1, const char *s2 )

Copies string s2 into array s1. The value of s1 is returned.

char *strncpy( char *s1, const char *s2, size_t n )

Copies at most n characters of string s2 into array s1. The value of s1 is returned.

char *strcat( char *s1, const char *s2 )

Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

char *strncat( char *s1, const char *s2, size_t n )

Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

 

 

Functions strcpy and strncpy

The example uses strcpy to copy the entire string in array x into array y and uses strncpy to copy the first 18 characters of array x into array z. A null character ('\0') is appended to array z, because the call to strncpy in the program does not write a terminating null character (the third argument is less than the string length of the second argument).

 

Example

/* how to use strcpy and strncpy */

#include <stdio.h>
#include<string.h>

int main ( void )
{
char x[] = "Happy Birthday to You"; /* initialize char array x */
char y[ 35 ]; /* create char array y */
char z[ 25 ]; /* create char array z */

/* copy contents of x into y */
printf( "%s%s\n%s%s\n",
"The string in array x is: ", x,
"The string in array y is: ", strcpy( y, x) );
/* copy first 14 characters of x into z. Does not copy null character */
strncpy( z, x, 18 );

z[ 18 ] = '\0'; /* terminate string in z */
printf( "The string in array z is: %s\n", z );

return 0;
}

Output :

The string in array x is: Many Happy Returns to You

The string in array y is: Many Happy Returns to You

The string in array z is: Many Happy Returns

 

 

Functions strcat and strncat

Function strcat appends its second argument (a string) to its first argument (a character array containing a string). The first character of the second argument replaces the null ('\0') that terminates the string in the first argument. You must ensure that the array used to store the first string is large enough to store the first string, the second string and the terminating null character copied from the second string.

 

Function strncat appends a specified number of characters from the second string to the first string. A terminating null character is automatically appended to the result. Following Example demonstrates function strcat and function strncat.

 

Example:

/* how to use strcat and strncat */

#include <stdio.h>
#include <string.h>

int main( void )
{
char s1[ 20 ] = "Happy "; /* initialize char array s1 */
char s2[] = "New Year "; /* initialize char array s2 */
char s3[ 40 ] = ""; /* initialize char array s3 to empty */

printf( "s1 = %s\ns2 = %s\n", s1, s2 );

/* concatenate s2 to s1 */
printf( "strcat( s1, s2 ) = %s\n",strcat(s1, s2) );
/* concatenate first 6 characters of s1 to s3. Place '\0' after
last character */
printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) );
/* concatenate s1 to s3 */
printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) );

return 0;
}

Output:

s1 = Happy

s2 = New Year

strcat( s1, s2 ) = Happy New Year

strncat( s3, s1, 6 ) = Happy

strcat( s3, s1 ) = Happy Happy New Year