Skip to content

Instantly share code, notes, and snippets.

@JesseHerrick
Last active December 19, 2015 12:19
Show Gist options
  • Save JesseHerrick/5954129 to your computer and use it in GitHub Desktop.
Save JesseHerrick/5954129 to your computer and use it in GitHub Desktop.
A simple replace example written in C++. Plus... fancy error control!
#include <iostream>
#include <string>
using namespace std;
// declare main function with argv abilities
int main(int argc, char* argv[]) {
// first string
string statement;
// to make a variable is nil error look cleaner
if (argv[1] != false) {
// if first arg exists... make it the statement variable
statement = argv[1];
}
else {
// if first arg doesn't exist... show this error instead
cout << "Please add command line arg.";
}
// replace string
string replace_statement = " there!";
// find string in statement variable
size_t lookfor_bash = statement.find('!');
// if the string we're looking for exists...
if (lookfor_bash != string::npos) {
// redefine statement variable with replace strings
statement = statement.replace(/* strings to replace */lookfor_bash, /* replace this many chars */replace_statement.length(), /* replace with this string */replace_statement);
}
cout << statement << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment