C++ Program to Find the Length of a String

In this article, you will learn how to find and print the length of any given string given by the user at run-time in the C++ language. The program was created with the help of these approaches:

In C++, find the length of a string without using the strlen() function

To find the length of a string in C++ programming, you have to ask the user to enter the string first. and then find its length as shown in the program given below.

This program finds the length of a string using user-based code. That is, this program does not use library functions or the built-in function strlen().

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[200];
    int len=0, i=0;
    cout<<"Enter the String: ";
    gets(str);
    while(str[i])
    {
        len++;
        i++;
    }
    cout<<"\nLength = "<<len;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program find length of string

Now enter any string, say codes cracker dot com, and press the ENTER key to find and print the length of the given string, as shown here in the following output:

find length of string without strlen c++

Here is another sample run with user input, fresherearth:

find length of string c++

The dry run of the above program with user input, "fresherearth" goes like this:

In C++, use the strlen() function to determine the length of a string

Here is another C++ program that also finds and prints the length of a string entered by the user. The only difference with the previous program is that this program uses a built-in or library function of C++ named strlen().

The function strlen() takes a string as its argument and returns its length. This function is defined in the string.h header file.

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[200];
    int len=0;
    cout<<"Enter the String: ";
    gets(str);
    len = strlen(str);
    cout<<"\nLength = "<<len;
    cout<<endl;
    return 0;
}

The output produced by this program is similar to that of previous programs.

In C++, find the length of a string using a pointer

Let's make the same program, but this time we'll use pointers to find the length of a string.The address of the first character of a string gets initialized to a pointer type variable, say ptr, and using this variable, the length of the string gets calculated.

Note: The & is called as the address of operator. Whereas as the * is called as the value at operator. The ptr++ (char pointer type variable) moves to the next character's address

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[200], *ptr;
    int len=0;
    cout<<"Enter the String: ";
    gets(str);
    ptr = &str[0];
    while(*ptr)
    {
        len++;
        ptr++;
    }
    cout<<"\nLength = "<<len;
    cout<<endl;
    return 0;
}

The same program in different languages

C++ Quiz


« Previous Program Next Program »