
- 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
Write a C program demonstrating strlen library function
The strlen () function
It returns the number of characters in a string.
Syntax
int strlen (string name)
In this program, with the help of gets function reading the name at run time and trying to print the length of that name using strlen() function, this function returns an integer value and try to print that no using printf.
Example 1
#include<stdio.h> #include<string.h> void main(){ //Declaring string and length// char name[25]; int length; //Reading Input from user// printf("Enter your name : "); gets(name); length=strlen(name); //Printing name// printf("Your name is : "); puts(name); printf("Length of the string is : %d
",length); }
Output
Enter your name : Tutorialspoint Your name is : Tutorialspoint Length of the string is : 14
We shall consider another example to print string length without using string function i.e., without using strlen().
Example 2
#include <stdio.h> int main(){ char string[50],i; printf("enter the string:
"); scanf("%s",string); for(i=0; string[i]!='\0'; ++i); printf("\length of the given string is: %d",i); return 0; }
Output
enter the string: TutorialsPoint length of the given string is: 14
- Related Articles
- Write a C program using time.h library function
- Write a C program demonstrating examples on pointers
- Write a C program to Reverse a string without using a library function
- Write a C program to compare two strings using strncmp library function
- What is strlen function in C language?
- strlen() function in PHP
- Write a C program of library management system using switch case
- Write an example JDBC program demonstrating the batch processing with statement object?
- Write an example JDBC program demonstrating the batch processing with PreparedStatement object?
- Write an example JDBC program demonstrating the batch processing with CallableStatement object?
- isgraph() C library function
- Difftime() C library function
- Write C program using isupper() function
- C program demonstrating the concepts of strings using Pointers
- Difference between strlen() and sizeof() for string in C Program

Advertisements