Find length of string

#include <iostream>
#include <string.h>

using namespace std;

int main(){
    string str = "OceanLabz";
    
    //By Function
    //Length of string using size() methode 
    cout<< str.size() <<endl;

    //Length of string using length methode 
    cout<< str.length() <<endl;


    //By logic
    
    //1
    int i=0;
    while(str[i])
        i++;
    cout<< i << endl;

    //2
    for( i = 0; str[i]; i++);
    cout<< i << endl;

    return 0;
}

    Leave a Reply

    Your email address will not be published.

    Need Help?