strlen() sample program in C++

By: Tamil Selvan Emailed: 1705 times Printed: 2207 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The most popular library is almost certainly the string library, with perhaps the function strlen() called most often. strlen() returns the length of a null-terminated string. Listing below illustrates its use.

strlen().

1:     #include <iostream.h>
2:     #include <string.h>
3:
4:     int main()
5:     {
6:        char buffer80];
7:        do
8:        {
9:           cout << "Enter a string up to 80 characters: ";
10:          cin.getline(buffer,80);
11:          cout << "Your string is " << strlen(buffer);
12:          cout << " characters long." << endl;
13:       }    while (strlen(buffer));
14:       cout << "\nDone." << endl;
15:     return 0;
16: }

Output: Enter a string up to 80 characters: This sentence has 31 characters
Your string is 31 characters long.
Enter a string up to 80 characters: This sentence no verb
Your string is 21 characters long.
Enter a string up to 80 characters:
Your string is 0 characters long.

Done.

Analysis: On line 6, a character buffer is created, and on line 9 the user is prompted to enter a string. As long as the user enters a string, the length of the string is reported on line 11.

Note the test in the do...while() statement: while (strlen(buffer)). Since strlen() will return 0 when the buffer is empty, and since 0 evaluates FALSE, this while loop will continue as long as there are any characters in the buffer.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

hiiiiiii

it's good turotiral for strlen()_ program

http://BharatClick.com


View Tutorial          By: Raju Singh at 2009-10-01 09:47:04
2. View Comment

Very good. very clear. thank you

View Tutorial          By: Andrei at 2012-12-16 04:29:36

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Tamil Selvan
Inner Class Example in Java
PreparedStatement Example in Java
UNIX read and write system calls sample program in C
Pointer Arrays and Pointers to Pointers in C
Relational and Logical Operators in C
Arithmetic Operators in C
strlen() sample program in C++
Sample program to draw a arc in J2ME
What Java Has Removed from C++
FileReader and FileWriter example program in Java

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using cout.width() in C++
Using cout.fill() in C++
Programming errors a compiler will detect in C++
Calculating total based on the given quantity and price in C++
File in C++ - Writing text to a file in C++
assert() example program in C++
Using peek() and putback() with cin in C++
Specifying default values to function parameters in C++
C++ Recursion function explained using Fibonacci series
Interface Versus Implementation in C++
C++ Sample Program for Allocating, using, and deleting pointers.
C ++ Reference Parameters Versus Java Reference Parameters
Converting C++ Multiple-Inheritance Hierarchies to Java
Printing values in DEBUG mode in C++
cin.ignore() in C++
Most Emailed Articles (in C++)
A Brief History of C++
atoi(), itoa() in C++
Character String Literals in C++
Lvalues and Rvalues in C++
Advanced if Statements in C++
Passing parameters to a function by value in C++
class keyword in C++
continue and break statements in C++
do...while Loops in C++
C++ Sample Program for Allocating, using, and deleting pointers.
Converting Pointers that Operate on Arrays in C++ to Java
Converting C++ Multiple-Inheritance Hierarchies to Java
ctime() sample program in C++
Implementing Pure Virtual Functions in C++
Call by reference in C++ Functions