Wednesday, August 29, 2012

Simple string program using string library

// simple string using c++ style
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "pine";
    string s2 = "apple";
    cout << "\n length of s1 is " << s1.length() << endl;
    string s3 = s1 + s2;
    cout << "\n s1 + s2 = " << s3 << endl;
    string s4 = s3.substr(2,4);
    cout << "\n s3.substr(2,4)= " << s4 << endl;
   
    cout << "\n s3.find(\"neap\")= " << s3.find("neap") << endl;
    cout << "\n s3.rfind('p')= " << s3.rfind('p') << endl;
    return 0;
}


OUTPUT:
 ----------------------------------------------------------------------------------------------

// to get line of text (that may include spaces) from the user
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cout << "\n type a string : ";
    getline(cin,str);
   
    cout << "\n you typed : " << str << endl;
    return 0;
}


OUTPUT:

No comments:

Post a Comment