Skip to content

Instantly share code, notes, and snippets.

@joshkunz
Created July 17, 2013 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkunz/6016412 to your computer and use it in GitHub Desktop.
Save joshkunz/6016412 to your computer and use it in GitHub Desktop.
/* Simple quoted translation program. It reads characters from
* stdin and writes them to standard out replacing all
* instances of the character 'WHAT' with the character 'WITH'.
* If a 'WHAT' character appears between 'QSTART' and 'QEND'
* (the starting and ending quote characters) it is not
* replaced. */
#include <stdio.h>
#include <stdbool.h>
#define QSTART '"'
#define QEND '"'
#define WHAT '|'
#define WITH ','
int main() {
char byte = 0;
bool quoted = false;
while (! feof(stdin)) {
fread(&byte, 1, 1, stdin);
if ((! quoted) && byte == QSTART) {
quoted = true;
} else if (quoted && byte == QEND) {
quoted = false;
}
if ((! quoted) && byte == WHAT) { byte = WITH; }
fwrite(&byte, 1, 1, stdout);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment