C/C++ strncpy - string copy up to n characters

<<Previous

Next >>




strcmp() - String Compare        strcpy() - String Copy        strcat() - String Concatenate




strncpy in C, C++ programming - Syntax

strncpy() function in C is used to copy the specified number of characters from source to destination.

Syntax:
	char *strcpy(char *dest, const char *src, size_t number);
For example:
strncpy(dest, source, 4) - this copies only 4 characters from source string to dest string.

Note: size_t treats as an integer type.

Example program using strncpy()

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


main()
{
  char source[]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};
  char dest[10]=" ";
  clrscr();
  printf("The source string is %s",source);
  strncpy(dest,source,4);
  printf("The destination string after copy %s",dest);
  return(0);
}

Output:

The source string is WELCOME
The destination string after copy WELC

strlen() - String Length strlwr() - String Lower       

strncat() - String n Concatenate        strncmp() - String n Compare       






<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C/C++ strncpy - string copy up to n characters

<<Previous

Next >>




strcmp() - String Compare        strcpy() - String Copy        strcat() - String Concatenate




strncpy in C, C++ programming - Syntax

strncpy() function in C is used to copy the specified number of characters from source to destination.

Syntax:
	char *strcpy(char *dest, const char *src, size_t number);
For example:
strncpy(dest, source, 4) - this copies only 4 characters from source string to dest string.

Note: size_t treats as an integer type.

Example program using strncpy()

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


main()
{
  char source[]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};
  char dest[10]=" ";
  clrscr();
  printf("The source string is %s",source);
  strncpy(dest,source,4);
  printf("The destination string after copy %s",dest);
  return(0);
}

Output:

The source string is WELCOME
The destination string after copy WELC

strlen() - String Length strlwr() - String Lower       

strncat() - String n Concatenate        strncmp() - String n Compare       






<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy