CPP Program to find length of string
Here,you will find the knowledge about how to find the length of the string or size of string in the cpp programming language.
To find the length of string object size () and length() both functions are used.They are synonyms to each other and exactly done same job.
Code:
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming";
// can use str.length()
cout << "String Length = " << str.length()<<endl;
//also use str.size()
cout << "String Length = " << str.size();
return 0;
}
Output:
String Length = 15
String Length = 15
In the cpp, another way to calculate the length by using a function from the library cstring
Code:
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char str[] = "C++ Programming";
cout << "String Length = " << strlen(str)<<endl;
return 0;
}
Output:
String Length = 15
Comments
Post a Comment