By: Tamil Selvan in C++ Tutorials on 2007年09月17日 [フレーム]
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C++ )
Calculate average using Two-Dimensional Array in C++
Calculating total based on the given quantity and price in C++
Compute the square root of the sum of the squares of an array in C++
Program to add two numbers in C++
Sorting an array of Strings in C++
Compiling and Linking Multiple Source Files in C++
Matrix using nested for loops in C++
Programming errors a compiler will detect in C++
Two-Dimensional Array Manipulation in C++
Using the Built-in Arithmetic Types in C++
Concatenated String Literals in C++
assert() example program in C++
Latest Articles (in C++)
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate