C strlen() and strcpy() function string functions

strlen() and strcpy() functions are two very useful string functions. We can determine the length of any string using strlen() function. strcpy() function is used to copy a string to another string. Here in this article we will learn about this two string functions which are strlen() and strcpy() as well as their use in C programming.

strlen() function in C, length of a string

The strlen() function can determine the length of a string and return the length of given string. It returns the length without counting the null character. We use strlen() function to determine the length of any string. Let’s consider the following example C program to understand about strlen() function.

// strlen() function in C, length of a string
#include<stdio.h>
#include <string.h>
int main(){
  char ch[50]={'C', 'o', 'm', 'p', 'e', 't', 'i', 't', 'i', 'v', 'e', '\0'};
    printf("Length of given string is = %d\n", strlen(ch));
  return 0;
}

Output of this strlen() functions program:

strlen() function in C, length of a string

strcpy() function in C, copy a string

We use strcpy() function to copy a string from any source to destination. At first we should know the syntax of strcpy() function. Here is the syntax of strcpy() function in C.

// syntax of strcpy() function

strcpy(destination, source);

Let’s see a C program of strcpy() function in C.

// strcpy() function in C, copy a string
#include<stdio.h>
#include <string.h>
int main(){
  char firstArray[20]={'C', 'o', 'm', 'p', 'e', 't', 'i', 't', 'i', 'v', 'e', '\0'};
  char secondArray[20];

  strcpy(secondArray, firstArray);

  printf("First string is : %s\n",firstArray);
  printf("Second string is : %s\n",secondArray);

return 0;
}

Program output:

strcpy() function in C, copy a string