CPP   19

strings find

Guest on 20th April 2022 02:01:56 AM

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. #include <string>
  6.  
  7. int main() {
  8.  
  9.     string sentence="I am a elephant";
  10.     int pos;
  11.     string animal_1="elephant";
  12.     string animal_2="cat";
  13.     int last_char;
  14.  
  15.     cout << sentence << endl;
  16.  
  17.     cout << "The sentence is " << sentence.length() << " characters long." << endl;
  18.  
  19.     pos=sentence.find(animal_1);
  20.  
  21.     cout << pos << endl;
  22.  
  23.     sentence.replace(pos,animal_2.length(),animal_2);
  24.  
  25.     last_char=sentence.length()-1;
  26.  
  27.     //sentence.erase(10,5);
  28.     int chop_mark=pos+animal_2.length();
  29.     int end_of_sentence=sentence.length()-1;
  30.     sentence.erase(chop_mark,end_of_sentence-chop_mark+1);
  31.     cout << sentence << endl;
  32.  
  33.     cout << "The sentence is " << sentence.length() << " characters long." << endl;
  34.  
  35.     cout << sentence << endl;
  36.  
  37. }

Raw Paste


Login or Register to edit or fork this paste. It's free.