Example 74: Common String Functions Implementation
Explore how to implement common string functions in C such as strlen to find string length, strcpy to copy strings, strcat to concatenate strings, and strcmp to compare strings. This lesson helps you understand and write these fundamental string manipulation routines using pointers and character arrays.
We'll cover the following...
We'll cover the following...
Problem
Write a program to implement the following functions:
- strlen: Finds the length of the string
- strcpy: Copies contents of one string to another string
- strcat: Appends one string at the end of another string
- 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 |