Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created November 5, 2012 08:00
Show Gist options
  • Save itsjohncs/4015915 to your computer and use it in GitHub Desktop.
Save itsjohncs/4015915 to your computer and use it in GitHub Desktop.
Classroom Session 6 - Johnnie Appleseed
#include <iostream>
using namespace std;
int main() {
string name = "Johnnie Appleseed's Amazing Fruit Stand!";
// What does each of these print out?
cout << name.find("Johnnie") << endl;
cout << name.find("s") << endl;
cout << name.find("Apple") << endl;
cout << name.find("x") << endl;
if (name.find("x") == -1) {
cout << "Where oh where has my x gone?" << endl;
} else {
cout << "I found an x!" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
string name = "Johnnie Appleseed's Amazing Fruit Stand!";
// What does each of these print out?
cout << name.substr(2) << endl;
cout << name.substr(4, 6) << endl;
cout << name.substr(5, 2) << endl;
cout << name.substr(name.size() - 1) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string name = "Johnnie Appleseed's Amazing Fruit Stand!";
// What does each of these print out?
cout << name.substr(name.find("Appleseed's")) << endl;
cout << name.substr(name.find("Amazing"), name.find("Fruit")) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment