

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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\n",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: \n"); 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 Questions & Answers
- 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
- strlen() function in PHP
- What is strlen function in C language?
- Write a C program of library management system using switch case
- Write C program using isupper() function
- isgraph() C library function
- Difftime() C library function
- 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?
- Write a C program to check the atexit() function
- C program demonstrating the concepts of strings using Pointers
Advertisements