Ad Code

Sub String Operation?

To use string data type in C++ we #include <string.h> ;   What are the sub-string operations we can perform on string data type in C++? as mentioned in the lecture if we want to search for second name "Ali" in an array of strings, how we do that?; kindly explain by code.


 string object containing a substring of the current object.

Example

// string::substr
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str="We think in generalities, but we live in details.";
                             // quoting Alfred N. Whitehead
  string str2, str3;
  size_t pos;

  str2 = str.substr (12,12); // "generalities"

  pos = str.find("live");    // position of "live" in str
  str3 = str.substr (pos);   // get from "live" to the end

  cout << str2 << ' ' << str3 << endl;

  return 0;
}
Reactions

Post a Comment

0 Comments