Answers for "print a part of a string c++"

C++
0

access part of string in c++

#include <iostream>
#include <string.h>

using namespace std;
int main() {
   string str1 = "Apples are red";
   string str2 = str1.substr(11, 3);
   string str3 = str1.substr(0, 6);

   cout << "Substring starting at position 11 and length 3 is: " << str2 <<endl;
   cout << "Substring starting at position 0 and length 6 is: " << str3;
   return 0;
}
Posted by: Guest on July-03-2021
0

how to get a section of a string in c++

// 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;
}
Posted by: Guest on February-10-2021

Code answers related to "print a part of a string c++"

Browse Popular Code Answers by Language