In my previous blog, I have given you an introduction to strings.Today I will discuss on some string characteristics or you can say,"string operations".
String length( )
String size( )
If you want to find the length of any string, then its syntax is as follows:
str.length( );
String length( )
If you want to find the length of any string, then its syntax is as follows:
str.length( );
String capacity( )
If you want to find the capacity of any string, then its syntax is as follows:
str.capacity( );
Maximum size( )
If you want to size the maximum size of any string, then its syntax is as follows:
str.max_size( );
String empty or not
If you want to check whether the string is empty or not,then its syntax is as follows:
(str.empty?"yes":"no") ; (it is same as conditional operator)
So, to implementation of these operations is as follows:
Source code
#include<iostream>
#include<string.h>
using namespace std;
main()
{
string s1;
cout<<"Enter any string:";
getline(cin,s1); // syntax for getting the input of string
cout<<"Size="<<s1.size()<<endl<<endl;
cout<<"Length="<<s1.length()<<endl<<endl;
cout<<"Capacity="<<s1.capacity()<<endl<<endl;
cout<<"Maximum size="<<s1.max_size()<<endl<<endl;
cout<<"Empty="<<(s1.empty()?"yes":"no")<<endl<<endl;
}
No comments:
Post a Comment