Example 74: Common String Functions Implementation

Problem

Write a program to implement the following functions:

  1. strlen: Finds the length of the string
  2. strcpy: Copies contents of one string to another string
  3. strcat: Appends one string at the end of another string
  4. strcmp: Compares two strings to find whether they are identical or not

Example

Suppose we have the following four strings:

  • s1[] = “kicit”
  • s2[] = “Nagpur”
  • s3[20] = “”
  • s4[] = “kicit”

These strings will be used as inputs for the examples given in the table below.

Function Input Output
strLen s1 5
strCpy s3, s1 kicit
strCat s3, s2 kicitNagpur
strCmp s1, s2 29
strCmp s1, s4 0

Try it yourself - strlen()

Try to calculate the length of a given string on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Get hands-on with 1400+ tech skills courses.