fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(){
  6. string s="Dog goes woof";
  7. cout<<"Довжина рядка: "<<s.length()<<endl;
  8.  
  9. s.append(", \nCat goes meow");
  10. cout<<s<<endl;
  11.  
  12. cout<<"Перший символ: "<<s.at(0)<<endl<<"Останній символ: "<<s.at(s.size()-1)<<endl;
  13.  
  14. size_t positionCat=s.find("Cat");
  15. cout<<"Позиція 'Cat': "<<positionCat<<endl;
  16.  
  17. size_t positionWoof=s.find("woof");
  18. string sound=s.substr(positionWoof,4);
  19. cout<<"Dog's sound: "<<sound<<endl;
  20.  
  21. size_t positionDog=s.find("Dog");
  22. s.replace(positionDog,3,"Fox");
  23. cout<<s<<endl;
  24.  
  25. s.insert(positionWoof,"loud ");
  26. cout<<s<<endl;
  27.  
  28.  
  29. if(positionCat != string::npos)s.erase(positionCat-2);
  30. cout<<s;
  31.  
  32. string chorus="Ring-ding-ding-ding-dingeringeding!";
  33. if(s.compare(chorus)==0)cout<<"Рядки однакові"<<endl;
  34. else cout<<"Рядки різні"<<endl;
  35.  
  36. s.clear();
  37. cout<<"Рядок очишений? "<<(s.empty()?"Так":"Ні");
  38.  
  39. }
  40.  
Success #stdin #stdout 0.01s 5328KB
stdin
9 8 7 6 5 4 3 2 1 0
stdout
Довжина рядка: 13
Dog goes woof, 
Cat goes meow
Перший символ: D
Останній символ: w
Позиція 'Cat': 16
Dog's sound: woof
Fox goes woof, 
Cat goes meow
Fox goes loud woof, 
Cat goes meow
Fox goes loud Рядки різні
Рядок очишений? Так