
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strlen function in C language?
The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
An array of characters is called a string.
Declaration
Given below is the declaration of an array −
char stringname [size];
For example − char a[50]; string of length 50 characters
Initialization
- Using single character constant −
char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char a[10] = "Hello":;
Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’
The strlen ( ) function
This function gives the length of the string, i.e., the number of characters in a string.
Syntax
The syntax of strlen() function is as follows −
int strlen (string name)
Sample program
The following program shows the usage of strlen() function.
#include <string.h> main ( ){ char a[30] = "Hello"; int l; l = strlen (a); printf ("length of the string = %d", l); getch ( ); }
Output
When the above program is executed, it produces the following result −
length of the string = 5 Note : "\0" not counted as a character.
Consider another example.
Example
Following is the C program to find the length of a string −
#include<stdio.h> #include<string.h> int main(){ int str1, str2; //initializing the strings char string1[] = "Welcome To"; char string2[] = {'T','U','T','O','R','I','A','L','\0'}; //calculating the length of the two strings str1 = strlen(string1); str2 = strlen(string2); printf("string1 length is: %d
", str1); printf("string2 length is: %d
", str2); }
Output
When the above program is executed, it produces the following result −
string1 length is: 10 string2 length is: 8
- Related Articles
- strlen() function in PHP
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok() function in C language?
- What is strtok_r() function in C language?
