String Handling Functions in C (strlen, strcpy, strcmp, strcat with Examples)
This tutorial explains the commonly used string handling functions in C, including strlen, strcpy, strcmp, and strcat. It provides syntax, examples, and practical usage to help beginners manipulate strings efficiently in C programming.
1. strlen() – String Length
Purpose: Returns the length of a string (excluding the null character \0).
Syntax:
Example:
Output:
2. strcpy() – Copy String
Purpose: Copies the source string into the destination string.
Syntax:
Example:
Output:
3. strcmp() – Compare Strings
Purpose: Compares two strings.
- Returns
0if strings are equal - Returns positive if first string > second
- Returns negative if first string < second
Syntax:
Example:
Output:
4. strcat() – Concatenate Strings
Purpose: Appends the source string to the end of the destination string.
Syntax:
Example:
Output:
5. Key Points to Remember
- Include
#include <string.h>to use string functions strlen()does not count null characterstrcpy()overwrites the destination stringstrcmp()compares ASCII values of charactersstrcat()requires enough space in the destination string