Characteristics of string and accessing characters from string in C++

Characteristics of string and accessing characters from string:- To represent characteristics and advantages of string, string class has many functions like size ( ), length( ), capacity( ), maxsize ( ), empty( ), reverse( ), upper( ), lower( ), substring( ) etc. Functions. In addition, it has different functions for accessingpresent characters like findfirstof( ), findlastof( ), at( ), find( ) etc. Are provided.

C++ program to display characteristics of string object.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(){
string s1="ONE TWO THREE FOUR";
cout<<"Original String-"<<endl<<"S1="s1<<endl;
cout<<"Characteristics of String:-"<<endl;
cout<<"Size="<<s1.size()<<endl;
cout<<"Length="<<s1.length()<<endl;
cout<<"Capacity="<<s1.capacity()<<endl;
cout<<"Maximum Size="<<s1.maxsize()<<endl;
cout<<"Empty string="<<s1.empty()<<endl;
cout<<"Resizing size of String="<<s1.resize(20)<<endl;
cout<<"Reverse="<<s1.reverse()<<endl;
cout<<"String Uppercase="<<s1.upper()<<endl;
cout<<"String Lowercase="<<s1.lower()<<endl;
cout<<"Substring="<<s1.substring(0,7)<<" Ka "<<s1.substring(14,17)<<endl;
//Accessing characters
cout<<"10th character of "<<s1<<" "<<s1.at(10)<<endl;
int x=s1.find("TWO");
cout<<"TWO is found at="<<x<<"position"<<endl;
x=s1.findfirstof("E");
cout<<"First occurrence of E is "<<x<<endl;
x=s1.findlastof("O");
cout<<"Last occurrence of O is "<<x <<endl;
getch();
}

No comments:

Post a Comment