Finding string class string length .length() .size() .c_str() methods – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#3009 siteicon   siteicon   siteicon  

Finding string class string length .length() .size() .c_str() methods

Program to demonstrate string class based string in c++ using different methods length() size() and conversion to c_str()

Learning Objectives

  • Finding number of characters in a string class based string using three different methods .length(), .size() and .c_str() in a null finding loop

Source Code

TC++ #3009

Source Code

Run Output

String = Trees can check pollution.
Length of string using .length() = 26
Length of string using .size() = 26
Length of string using null finding = 26

Code Understanding

string s1=”Trees can check pollution.”;
string s1 object has been initialised.

cout<<“String = “<<s1<<endl;
s1 object printed directly using cout

cout<<“Length of string using <string object>.length() = “<<s1.length()<<endl;
Here we find the number of characters in the string using length() method.

cout<<“Length of string using <string object>.size() = “<<s1.size()<<endl;
Here we find the number of characters using the size() method. length() and size() method are synonyms.

int len;
Here we run the loop to find the null after converting the string class based string to older c-style string using the function c_str().

cout<<“Length of string using null finding = “<<len<<endl;
Here we print the length using the older technique


  • After c++11 standard on wards the null at the end of string class based string can also be tested directly without converting it using c_str() method.
  • string class based string may not be supported on older DOS based compilers.


Common Errors

  • Student often forget to put round braces around methods of string object. for example s1.length is not correct. one has to write s1.length()

Suggested Filename(s): str-length-size.cpp,strlensz.cpp


sunmitra| Created: 25-Jan-2018 | Updated: 25-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues