on Leave a Comment

C++ Program to Find the Length of a String

To understand C++ Program to Find the Length of a String. You should learn C++ String - String Class and Char Array.

Topics:

(1) C++ Program to Find the Length of a C Style String

(2) C++ Program to Find the Length of a String Object

C++ Program to Find the Length of a C-Style String

Example 1:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char str[]="PC TECHNICAL PRO";
    cout<<"Length of String is "<<strlen(str);
    return 0;
}

OUTPUT:

Length of String is 16

Example 2:

#include <iostream>
using namespace std;
int main()
{
    char str[]="PC TECHNICAL PRO";
    int i=0;
    while(str[i]!='\0')
    {
        i++;
    }
    cout<<"Length is "<<i;
    return 0;
}

OUTPUT:

Length is 16

C++ Program to Find the Length of a String Object

#include <iostream>
using namespace std;
int main()
{
    string str="PC TECHNICAL PRO";
    cout<<"Length of String is "<<str.length();
    return 0;
}

OUTPUT:

Length of String is 16

0 comments:

Post a Comment