How To Find The Length Of A String In C++

Have you ever seen a string and counted the number of characters in it just for fun? Pretty easy right?
But what about a program? How can a C++ program do that?
And that’s why today we’ll be finding the solution to How to find the length of a string in C++.

Find The Length Of A String In C++

There are 2 ways to solve this:

  1. The Simple Method
  2. Using String Length Function

I’ll be explaining both of them so you can implement whichever you find easier.

The Simple Method: length of a string in C++

Step 1:
Including Header files
In this method, we would only be needing the basic header file i.e. ‘iostream’
I’m also writing ‘using namespace std;’ to prevent ‘std::’ from being written before every cin or cout. Consider it a shortcut

#include <iostream>
using namespace std;

 

Step 2:
Here we would be getting the string whose length you want to find from the user.
I’m storing the string in ‘str’ of type string.

string str;
cout<<"Enter a string: ";
cin>>str;

 

Step 3:
This is where the main operations happen
Firstly I’m declaring 2 variables ‘i‘ and ‘count‘ and initializing them both to 0.
Here I’m using ‘i’ to extract each character from the string and I’m using ‘count’ to count the number of characters.
I’m using a while loop with the condition set to (str[i]!=’\0′)

\0 means NULL. Basically when you click enter on the keyboard to submit a string, after the string a ‘\0’ is inserted. It holds no value and is basically used to tell that the string ends at this point

int i=0, count=0;   //i for extracting character and count for counting
while(str[i]!='\0') //while character is not end line
{
    count++;        //Counting number of characters in the string
    i++;            //Incrementing to see the next character.
}

 

Step 4:
And Now we print the length of the string that is stored in count by using the following function:

cout<<"Length is:"<<count;

 

If you have understood the procedure your code should look something like this:

#include <iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter a string: ";
    cin>>str;
    int i=0, count=0;   //i for extracting character and count for counting
    while(str[i]!='\0') //while character is not end line
    {
        count++;        //Counting number of characters in the string
        i++;            //Incrementing to see the next character.
    }
    cout<<"Length is:"<<count;
}

 

Here is how the output should look:

Enter a string: CodeSpeedy                                                                                                                    
Length is:10

Using the String Length Function: length()

In This, we will use a predefined string function called ‘length()’
It takes a string as a parameter and returns an integer that is the length

The Syntax is as follows:

int length_of_string = string_name.length();

While doing this method only your Step 1 would be identical to the simple method i.e. accepting the string and storing it in str of type string

 

Step 2:
Here I declared a variable len of type int that would hold the length of the string in it.

int len;

 

Step 3:
Here we use the function length by substituting the variable name in the syntax
Quick reminder, str is the variable holding the string and len is a variable that accepts the result of the function that would hold the length of the string

len = str.length();

 

Step 4:
And finally, we now print ‘len’ i.e. length of the string

cout<<"Length is:"<<len;

 

If you’ve followed me until here then your source code should look something like this:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cout<<"Enter a string: ";
    cin>>str;
    int len;
    len = str.length();
    cout<<"Length is:"<<le
}

Also read:

Leave a Reply

Your email address will not be published.