Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Strings – string class type
Concept Learning Code Sheets

Strings with string class declaration, initialisation and assignment #3006

A program to demonstrate declaration and initialisation of strings in c++ using the string class.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
s1="C++ string class is easy to comprehend without a predefined size";
cout<<s1<<endl;
string s2="Can be initialised by assignment";
cout<<s2<<endl;
string s3("Initilisation like a function is also allowed");
cout<<s3<<endl;
return 0;
}

Finding string class string length .length() .size() .c_str() methods #3009

Program to demonstrate string class based string in c++ using different methods length() size() and conversion to c_str()

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1="Trees can check pollution.";
cout<<"String = "<<s1<<endl;
cout<<"Length of string using <string object>.length() = "<<s1.length()<<endl;
cout<<"Length of string using <string object>.size() = "<<s1.size()<<endl;
int len;
for(len=0;s1.c_str()[len]!='';++len);
cout<<"Length of string using null finding = "<<len<<endl;
return 0;
}

User input to string class based strings #3023

Collecting user input to string class string objects using cin and getline()

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout<<"Enter a sentence : ";
cin>>s;
cout<<s<<endl;
cin.ignore(250,'n');
cout<<"Enter another sentence : ";
getline(cin,s);
cout<<s<<endl;
return 0;
}

Copy and Addition (Concatenation) of string class based strings #3028

A demonstration program to see how string objects of string class are copied or concatenated one after the another.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s2,s3;
s1="Welcome";
s2=s1;
s3=s2+" to the world of c++";
cout<<s3<<endl;
return 0;
}

Use of clear() and empty() methods of string class object #3031

Learn to use methods <string object>.clear() and <string object>.empty()

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
s="C++ string";
s.clear();
cout<<"Nothing will print after : if the string is cleared:"<<s<<endl;
if(s.empty()) cout<<"Successfully emptied"<<endl;
return 0;
}

accessing a character of object of string class #3045

A program to show individual characters in a string class based object are accessed.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string m="Yoga is a way of life";
cout<<m[0]<<m[1]<<m[2]<<m[3]<<endl;
cout<<m.at(0)<<m.at(1)<<m.at(2)<<m.at(3)<<endl;
return 0;
}

reversing a string object using the reverse function in algorithm lib #3049

Using the reverse() function directly along with begin() and end() iteration location setting functions of string class object

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string st="chocolates are tasty";
reverse(st.begin(),st.end());
cout<<st<<endl;
return 0;
}

palindrome check on string object using the reverse function in algorithm lib #3052

Using the reverse() function on string object to check for palindrome nature of a string class object.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s1="malayalam";
string s2=s1;
reverse(s2.begin(),s2.end());
if(s1.compare(s2)==0) cout<<"Palindrome"<<endl;
else cout<<"Not a Palindrome"<<endl;
return 0;
}



Code Sheets:8 
Back